AzureFunctions.Autofac 3.0.2-beta

This is a prerelease version of AzureFunctions.Autofac.
There is a newer version of this package available.
See the version list below for details.
dotnet add package AzureFunctions.Autofac --version 3.0.2-beta
NuGet\Install-Package AzureFunctions.Autofac -Version 3.0.2-beta
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureFunctions.Autofac" Version="3.0.2-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureFunctions.Autofac --version 3.0.2-beta
#r "nuget: AzureFunctions.Autofac, 3.0.2-beta"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AzureFunctions.Autofac as a Cake Addin
#addin nuget:?package=AzureFunctions.Autofac&version=3.0.2-beta&prerelease

// Install AzureFunctions.Autofac as a Cake Tool
#tool nuget:?package=AzureFunctions.Autofac&version=3.0.2-beta&prerelease

Autofac Dependency Injection in Azure Functions

An Autofac based implementation of Dependency Injection based on Boris Wilhelm's azure-function-dependency-injection and Scott Holden's WebJobs.ContextResolver available on NuGet as AzureFunctions.Autofac

Usage

In order to implement the dependency injection you have to create a class to configure DependencyInjection and add an attribute on your function class.

Configuration

Create a class and add a constructor that takes 1 string argument. The string argument will automatically be passed and runtime and simply needs to be passed through to the initialize method. In the constructor call the DependencyInjection.Initialize method. Perform the registrations as you normally would with Autofac.

    public class DIConfig
    {
        public DIConfig(string functionName)
        {
            DependencyInjection.Initialize(builder =>
            {
                //Implicity registration
                builder.RegisterType<Sample>().As<ISample>();
                //Explicit registration
                builder.Register<Example>(c => new Example(c.Resolve<ISample>())).As<IExample>();
                //Registration by autofac module
                builder.RegisterModule(new TestModule());
                //Named Instances are supported
                builder.RegisterType<Thing1>().Named<IThing>("OptionA");
                builder.RegisterType<Thing2>().Named<IThing>("OptionB");
            }, functionName);
        }
    }

Function Attribute and Inject Attribute

Once you have created your config class you need to annotate your function class indicating which config to use and annotate any parameters that are being injected. Note: All injected parameters must be registered with the autofac container in your resolver in order for this to work.

    [DependencyInjectionConfig(typeof(DIConfig))]
    public class GreeterFunction
    {
        [FunctionName("GreeterFunction")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, 
                                              TraceWriter log, 
                                              [Inject]IGreeter greeter, 
                                              [Inject]IGoodbyer goodbye)
        {
            log.Info("C# HTTP trigger function processed a request.");
            return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()}");
        }
    }

Using Named Dependencies

Support has been added to use named dependencies. Simple add a name parameter to the Inject attribute to specify which instance to use.

    [DependencyInjectionConfig(typeof(DIConfig))]
    public class GreeterFunction
    {
        [FunctionName("GreeterFunction")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, 
                                              TraceWriter log, 
                                              [Inject]IGreeter greeter, 
                                              [Inject("Main")]IGoodbyer goodbye, 
                                              [Inject("Secondary")]IGoodbyer alternateGoodbye)
        {
            log.Info("C# HTTP trigger function processed a request.");
            return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()} or {alternateGoodbye.Goodbye()}");
        }
    }

Multiple Dependency Injection Configurations

In some cases you may wish to have different dependency injection configs for different classes. This is supported by simply annotating the other class with a different dependency injection config.

    [DependencyInjectionConfig(typeof(DIConfig))]
    public class GreeterFunction
    {
        [FunctionName("GreeterFunction")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, 
                                              TraceWriter log, 
                                              [Inject]IGreeter greeter, 
                                              [Inject]IGoodbyer goodbye)
        {
            log.Info("C# HTTP trigger function processed a request.");
            return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()}");
        }
    }

    [DependencyInjectionConfig(typeof(SecondaryConfig))]
    public class SecondaryGreeterFunction
    {
        [FunctionName("SecondaryGreeterFunction")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, 
                                              TraceWriter log, 
                                              [Inject]IGreeter greeter, 
                                              [Inject]IGoodbyer goodbye)
        {
            log.Info("C# HTTP trigger function processed a request.");
            return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()}");
        }
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AzureFunctions.Autofac:

Package Downloads
Nadobe.Common

Common library for Nadobe evidence source implementations

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.3 280,540 6/16/2021
4.0.2 194,600 12/8/2020
4.0.2-alpha 346 11/24/2020
4.0.1 58,256 10/15/2020
4.0.1-alpha 347 10/14/2020
4.0.0 283,588 4/2/2020
4.0.0-alpha 739 3/8/2020
3.2.0.1-alpha 550 2/17/2020
3.2.0 43,628 2/18/2020
3.2.0-alpha 526 2/17/2020
3.1.0 13,916 1/22/2020
3.0.7 201,339 4/22/2019
3.0.6 174,231 11/5/2018
3.0.5 229,071 9/18/2018
3.0.4 9,066 8/31/2018
3.0.4-beta1 969 8/31/2018
3.0.3 2,417 8/26/2018
3.0.2 4,169 8/3/2018
3.0.2-beta04 716 8/1/2018
3.0.2-beta03 731 8/1/2018
3.0.2-beta02 739 8/1/2018
3.0.2-beta 740 8/1/2018
3.0.1 6,004 6/19/2018
3.0.0 12,283 6/10/2018
2.1.0 35,495 4/6/2018
2.0.0 18,854 11/3/2017
1.1.0 1,114 10/28/2017

Fixed a bug that was causing DI resolution to fail when class name and function name did not match