NOTE: Starting with NServiceBus version 8, the dependency injection container adapter packages are no longer required. NServiceBus directly supports the Microsoft.Extensions.DependencyInjection.Abstractions model and third party containers can be integrated using the externally-managed container mode. Visit the dependency injection upgrade guide for further information.
NServiceBus can be configured to use Simple Injector for dependency injection.
endpointConfiguration.UseContainer<SimpleInjectorBuilder>();
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
container.Options.AllowOverridingRegistrations = true;
container.Options.AutoWirePropertiesImplicitly();
container.Register(
instanceCreator: () =>
{
return new MyService
{
Property = "Created outside"
};
},
lifestyle: Lifestyle.Scoped);
endpointConfiguration.UseContainer<SimpleInjectorBuilder>(
customizations =>
{
customizations.UseExistingContainer(container);
});
DependencyLifecycle
maps to Simple Injector lifestyles as follows:
DependencyLifecycle |
Simple Injector lifestyle |
---|---|
InstancePerCall | Transient |
InstancePerUnitOfWork | Scoped |
SingleInstance | Singleton |