AzureFunctions.Extensions.DependencyInjection 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AzureFunctions.Extensions.DependencyInjection --version 1.1.0
NuGet\Install-Package AzureFunctions.Extensions.DependencyInjection -Version 1.1.0
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.Extensions.DependencyInjection" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureFunctions.Extensions.DependencyInjection --version 1.1.0
#r "nuget: AzureFunctions.Extensions.DependencyInjection, 1.1.0"
#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.Extensions.DependencyInjection as a Cake Addin
#addin nuget:?package=AzureFunctions.Extensions.DependencyInjection&version=1.1.0

// Install AzureFunctions.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=AzureFunctions.Extensions.DependencyInjection&version=1.1.0

Dependency Injection in Azure Functions using Microsoft.Extensions.DependencyInjection

A Microsoft.Extensions.DependencyInjection implementation based on the Autofac based implmenetation of CJ van der Smissen AzureFunctions.AutoFac

AzureFunctions.AutoFac on NuGet

AzureFunctions.Extensions.DependencyInjection on NuGet

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 Startup
{
    public Startup()
    {
        if(!DependencyInjection.IsInitialized)
        {
            DependencyInjection.Initialize(ConfigureServices);

            // Some other stuff such as AutoMapper.
            Mapper.Initialize(c =>
            {
                AzureFunctionsAutoMapperConfig.Initialize(c);
                BusinessAutoMapperConfig.Initialize(c);
            });
        }            
    }

    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IQueryDispatcher, QueryDispatcher>();
        services.AddBusinessLayer();
    }
}

Function Attribute and Inject Attribute

Once you have created your Startup or 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 IServiceCollection in order for this to work.

[ConfigureServices(typeof(Startup))]
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 [Unsupported]

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

[ConfigureServices(typeof(Startup))]
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, // Unsupported
        [Inject("Secondary")]IGoodbyer alternateGoodbye // Unsupported
    )
    {
        log.Info("C# HTTP trigger function processed a request.");
        return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()} or {alternateGoodbye.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 net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 is compatible.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 963,069 5/23/2018
1.1.2 25,794 4/26/2018
1.1.1 1,344 4/26/2018
1.1.0 5,094 2/15/2018
1.0.0 6,050 2/11/2018

Targeting multiple frameworks NET462, NET471 and NETSTANDARD2.0