AzureFunctions.Autofac
3.0.7
See the version list below for details.
dotnet add package AzureFunctions.Autofac --version 3.0.7
NuGet\Install-Package AzureFunctions.Autofac -Version 3.0.7
<PackageReference Include="AzureFunctions.Autofac" Version="3.0.7" />
paket add AzureFunctions.Autofac --version 3.0.7
#r "nuget: AzureFunctions.Autofac, 3.0.7"
// Install AzureFunctions.Autofac as a Cake Addin #addin nuget:?package=AzureFunctions.Autofac&version=3.0.7 // Install AzureFunctions.Autofac as a Cake Tool #tool nuget:?package=AzureFunctions.Autofac&version=3.0.7
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
The configuration class is used to setup dependency injestion. Within the constructor of the class DependencyInjection.Initialize must be invoked. Registrations are then according to standard Autofac procedures.
In both .NET Framework and .NET Core a required functionName parameter is automatically injected for you but you must specify it as a constructor parameter.
In .NET Core you have an optional baseDirectory parameter that can be used for loading external app configs. If you wish to use this functionality then you must specify this as a constructor parameter and it will be injected for you.
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,
ILogger log,
[Inject]IGreeter greeter,
[Inject]IGoodbyer goodbye)
{
log.LogInformation("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,
ILogger log,
[Inject]IGreeter greeter,
[Inject("Main")]IGoodbyer goodbye,
[Inject("Secondary")]IGoodbyer alternateGoodbye)
{
log.LogInformation("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,
ILogger log,
[Inject]IGreeter greeter,
[Inject]IGoodbyer goodbye)
{
log.LogInformation("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,
ILogger log,
[Inject]IGreeter greeter,
[Inject]IGoodbyer goodbye)
{
log.LogInformation("C# HTTP trigger function processed a request.");
return request.CreateResponse(HttpStatusCode.OK, $"{greeter.Greet()} {goodbye.Goodbye()}");
}
}
Verifying dependency injection configuration
Dependency injection is a great tool for creating unit tests. But with manual configuration of the dependency injection, there is a risk of mis-configuration that will not show up in unit tests. For this purpose, there is the DependencyInjection.VerifyConfiguration
method.
It is not recommended to call VerifyConfiguration
unless done so in a test-scenario.
VerifyConfiguration
verifies the following rules:
- That an
InjectAttribute
is preceeded by aDependencyInjectionConfigAttribute
. - That the configuration can be instantiated.
- That all injected dependencies in the given type can be resolved with the defined configuration.
- Optionally that no redundant configurations exist, i.e. a
DependencyInjectionConfigAttribute
with no correspondingInjectAttribute
.
Simple example of verification
Below is a very simple example of verifying the dependency injection configuration for a specific class:
DependencyInjection.VerifyConfiguration(typeof(MyCustomClassThatUsesDependencyInjection));
Ignoring redundant configurations
If you don't want to verify rule 4, pass in false
as the second parameter to VerifyConfiguration
:
DependencyInjection.VerifyConfiguration(typeof(MyCustomClassThatUsesDependencyInjection), false);
Example unit test to verify an entire project/assembly
For instance, you can use it in a unit test to verify that all classes in your project has dependency injection set up correctly:
[TestMethod]
public void TestDependencyInjectionConfigurationInAssembly() {
var assembly = typeof(SomeClassInYouProject).Assembly;
var types = assembly.GetTypes();
foreach (var type in types) {
DependencyInjection.VerifyConfiguration(type);
}
}
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 | 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. |
-
.NETFramework 4.6
- Autofac (= 4.2.1)
- Microsoft.Azure.WebJobs (>= 2.2.0)
-
.NETStandard 2.0
- Autofac (= 4.2.1)
- Microsoft.Azure.WebJobs (>= 3.0.0)
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 | 358,620 | 6/16/2021 | |
4.0.2 | 211,957 | 12/8/2020 | |
4.0.2-alpha | 385 | 11/24/2020 | |
4.0.1 | 61,399 | 10/15/2020 | |
4.0.1-alpha | 386 | 10/14/2020 | |
4.0.0 | 299,981 | 4/2/2020 | |
4.0.0-alpha | 821 | 3/8/2020 | |
3.2.0.1-alpha | 683 | 2/17/2020 | |
3.2.0 | 45,815 | 2/18/2020 | |
3.2.0-alpha | 570 | 2/17/2020 | |
3.1.0 | 14,127 | 1/22/2020 | |
3.0.7 | 205,977 | 4/22/2019 | |
3.0.6 | 178,143 | 11/5/2018 | |
3.0.5 | 238,540 | 9/18/2018 | |
3.0.4 | 9,396 | 8/31/2018 | |
3.0.4-beta1 | 1,027 | 8/31/2018 | |
3.0.3 | 2,467 | 8/26/2018 | |
3.0.2 | 4,309 | 8/3/2018 | |
3.0.2-beta04 | 741 | 8/1/2018 | |
3.0.2-beta03 | 776 | 8/1/2018 | |
3.0.2-beta02 | 788 | 8/1/2018 | |
3.0.2-beta | 790 | 8/1/2018 | |
3.0.1 | 6,073 | 6/19/2018 | |
3.0.0 | 12,644 | 6/10/2018 | |
2.1.0 | 36,231 | 4/6/2018 | |
2.0.0 | 19,774 | 11/3/2017 | |
1.1.0 | 1,172 | 10/28/2017 |
Provide functions base directory to InjectionConfig