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
In order to implement the dependency injection you have to create a class to configure DependencyInjection and add an attribute on your function class.
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");
});
}
}
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()}");
}
}
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()}");
}
}