Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Autofac

NuGet Package: NServiceBus.Autofac (7.x)
Target Version: NServiceBus 7.x

Starting with NServiceBus version 7, the dependency injection container adapter packages are no longer required. NServiceBus directly supports the Microsoft.Extensions.DependencyInjection model and third party containers can be integrated using the NServiceBus.Extensions.DependencyInjection package or via the .NET Generic Host.

In NServiceBus version 8, NServiceBus will no longer provide adapters for external dependency injection containers.

Visit the dependency injection upgrade guide for further information.

NServiceBus can be configured to use Autofac for dependency injection.

Default usage

endpointConfiguration.UseContainer<AutofacBuilder>();

Using an existing container

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyService());
var container = builder.Build();
endpointConfiguration.UseContainer<AutofacBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingLifetimeScope(container);
    });
Although it is possible to update the container after passing it to NServiceBus using the ContainerBuilder.Update method, from Autofac 4.2.1 onwards that method is marked as obsolete. It is recommended not to use this method to update the container after it has been passed to NServiceBus.

DependencyLifecycle mapping

DependencyLifecycle maps to Autofac instance scopes as follows:

DependencyLifecycleAutofac instance scope
InstancePerCallInstance Per Dependency
InstancePerUnitOfWorkInstance Per Lifetime Scope
SingleInstanceSingle Instance

Property Injection

This dependency injection container adapter automatically enables property injection for known types. Use the Func overload of .ConfigureComponent to get full control over the injected properties if needed.

Samples