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