Autofac

Project Hosting
NuGet Package NServiceBus.Autofac (6.x)
Target NServiceBus Version: 6.x

NServiceBus can be configured to use Autofac as a dependency injection container.

Default Usage

endpointConfiguration.UseContainer<AutofacBuilder>();

Existing Container Instance

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyService());
var container = builder.Build();
endpointConfiguration.UseContainer<AutofacBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingLifetimeScope(container);
    });
As of Autofac 4.2.1, the ContainerBuilder.Update method is marked as obsolete. In the future, updating the container passed to NServiceBus may not be possible.

DependencyLifecycle Mapping

The DependencyLifecycle map to Autofac in the following way.

DependencyLifecycleAutofac Equivalent
InstancePerCallInstance Per Dependency
InstancePerUnitOfWorkInstance Per Lifetime Scope
SingleInstanceSingleInstance

Last modified