AzureFunctions.Autofac 2.0.0

Autofac Implementation of DI for Azure Functions

There is a newer version of this package available.
See the version list below for details.
Install-Package AzureFunctions.Autofac -Version 2.0.0
dotnet add package AzureFunctions.Autofac --version 2.0.0
<PackageReference Include="AzureFunctions.Autofac" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureFunctions.Autofac --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AzureFunctions.Autofac, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=2.0.0

// Install AzureFunctions.Autofac as a Cake Tool
#tool nuget:?package=AzureFunctions.Autofac&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 call the DependencyInjection.Initialize method. Perform the registrations as you normally would with Autofac.

    public class DIConfig
    {
        public DIConfig()
        {
            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");
            });
        }
    }

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()}");
        }
    }

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 call the DependencyInjection.Initialize method. Perform the registrations as you normally would with Autofac.

    public class DIConfig
    {
        public DIConfig()
        {
            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");
            });
        }
    }

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()}");
        }
    }

Release Notes

Changed configuration method to be simplified.
Added support for named instances in autofac.

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 History

Version Downloads Last updated
4.0.2 32,155 12/8/2020
4.0.2-alpha 162 11/24/2020
4.0.1 21,018 10/15/2020
4.0.1-alpha 184 10/14/2020
4.0.0 118,446 4/2/2020
4.0.0-alpha 512 3/8/2020
3.2.0.1-alpha 340 2/17/2020
3.2.0 23,863 2/18/2020
3.2.0-alpha 341 2/17/2020
3.1.0 8,479 1/22/2020
3.0.7 156,561 4/22/2019
3.0.6 150,400 11/5/2018
3.0.5 32,463 9/18/2018
3.0.4 7,653 8/31/2018
3.0.4-beta1 756 8/31/2018
3.0.3 1,868 8/26/2018
3.0.2 3,434 8/3/2018
3.0.2-beta04 468 8/1/2018
3.0.2-beta03 465 8/1/2018
3.0.2-beta02 478 8/1/2018
3.0.2-beta 462 8/1/2018
3.0.1 5,189 6/19/2018
3.0.0 9,882 6/10/2018
2.1.0 26,656 4/6/2018
2.0.0 13,851 11/3/2017
1.1.0 619 10/28/2017
Show less