Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
- Documentation - .NET Core Integration
- Documentation - ASP.NET Core Integration
- NuGet
- Contributing - You can report problems and feature requests creating issues and pull requests on this project.
This quick start shows how to use the UseAutofacServiceProviderFactory
integration to help automatically build the root service provider for you.
- Reference the
Autofac.Extensions.DependencyInjection.AzureFunctions
package from NuGet. - Create your
Startup
class, following the documentation on how to Use dependency injection in .NET Azure Functions - In your
Configure
method, where you configure theIFunctionsHostBuilder
, callUseAutofacServiceProviderFactory(ConfigureContainer)
to hook Autofac into the startup pipeline. - In the
ConfigureContainer
method of yourStartup
class register things directly into an AutofacContainerBuilder
. - If you want to use functions declared in referenced projects, add
<FunctionsInDependencies>true</FunctionsInDependencies>
to the<PropertyGroup>
of your main Azure Functions project.
The IServiceProvider
will automatically be created for you, so there's nothing you have to do but register things.
[assembly: FunctionsStartup(typeof(Startup))]
public class Startup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder
.UseAppSettings() // this is optional, this will bind IConfiguration in the container.
.UseAutofacServiceProviderFactory(ConfigureContainer);
}
private void ConfigureContainer(ContainerBuilder builder)
{
builder
.Register(activator =>
{
// Example on how to bind settings from appsettings.json
// to a class instance
var mySettings = new MySettings();
var config = activator.Resolve<IConfiguration>();
config.GetSection(nameof(MySettings)).Bind(mySettings);
return mySettings;
})
.AsSelf()
.SingleInstance();
// Register all functions that resides in a given namespace
// The function class itself will be created using autofac
builder
.RegisterAssemblyTypes(typeof(Startup).Assembly)
.InNamespace("MyNamespace.Functions")
.AsSelf() // Azure Functions core code resolves a function class by itself.
.InstancePerTriggerRequest() // This will scope nested dependencies to each function execution
builder
.RegisterAssemblyTypes(typeof(Startup).Assembly)
.InNamespace("MyNamespace.Services")
.AsImplementedInterfaces()
.InstancePerTriggerRequest();
}
}
This is a basic function example, observe that classes and functions are not declared as static
:
public class Function1 : Disposable
{
public Function1(IService1 service1, ILogger logger)
{
// ...
}
[FunctionName(nameof(Function1))]
public async Task Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")]string myQueueItem)
{
await Task.Delay(2000);
_logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
// ...
}
Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.