AzureFunctions.Extensions.DependencyInjection
1.1.0
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
<PackageReference Include="AzureFunctions.Extensions.DependencyInjection" Version="1.1.0" />
paket add AzureFunctions.Extensions.DependencyInjection --version 1.1.0
#r "nuget: AzureFunctions.Extensions.DependencyInjection, 1.1.0"
// 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 | Versions 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. |
-
.NETFramework 4.6.2
- Microsoft.Azure.WebJobs (>= 2.1.0)
- Microsoft.Extensions.DependencyInjection (>= 2.0.0)
-
.NETFramework 4.7.1
- Microsoft.Azure.WebJobs (>= 2.1.0)
- Microsoft.Extensions.DependencyInjection (>= 2.0.0)
-
.NETStandard 2.0
- Microsoft.Azure.WebJobs (>= 3.0.0-beta4)
- Microsoft.Extensions.DependencyInjection (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Targeting multiple frameworks NET462, NET471 and NETSTANDARD2.0