NServiceBus automatically registers and invokes message handlers, sagas, and other user-provided extension points using a dependency injection container.
Container configuration
Built-in default container
NServiceBus has a built-in default container with an API for registration of user types. The following dependency lifecycles are supported:
Instance per call
A new instance will be returned for each call.
Represented by the enum value DependencyLifecycle.
.
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerCall);
});
or using a delegate:
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent(
componentFactory: () =>
{
return new MyService();
},
dependencyLifecycle: DependencyLifecycle.InstancePerCall);
});
Instance per unit of work
The instance will be a singleton for the duration of the unit of work. In practice this means the processing of a single transport message.
Represented by the enum value DependencyLifecycle.
.
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerUnitOfWork);
});
or using a delegate:
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent(
componentFactory: () =>
{
return new MyService();
},
dependencyLifecycle: DependencyLifecycle.InstancePerUnitOfWork);
});
Single instance
The same instance will be returned each time.
Represented by the enum value DependencyLifecycle.
.
SingleInstance
components that have dependencies that are scoped InstancePerCall
or InstancePerUnitOfWork
will still resolve. In effect, these dependencies, while not scoped as SingleInstance
, will behave as if they are SingleInstance
because the instances will exist inside the parent component.endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.SingleInstance);
});
or using a delegate:
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.ConfigureComponent(
componentFactory: () =>
{
return new MyService();
},
dependencyLifecycle: DependencyLifecycle.SingleInstance);
});
or using the explicit singleton API:
endpointConfiguration.RegisterComponents(
registration: configureComponents =>
{
configureComponents.RegisterSingleton(new MyService());
});
Using a third party container
NServiceBus also supports the following third party containers:
Plugging in other containers
If a specific library is not supported, create a plugin using the IContainer
abstraction. Once this is created and registered, NServiceBus will use the custom dependency injection to look up its own dependencies.
Create a class that implements 'IContainer':
public class MyContainer :
IContainer
{
Create a class that implements 'ContainerDefinition' and returns the 'IContainer' implementation:
public class MyContainerDefinition :
ContainerDefinition
{
public override IContainer CreateContainer(ReadOnlySettings settings)
{
return new MyContainer();
}
}
Then register the ContainerDefinition
to be used:
endpointConfiguration.UseContainer<MyContainerDefinition>();
Injecting the endpoint instance
IEndpointInstance
is not registered automatically and must be registered explicitly to be injected.
IEndpointInstance
is not IDisposable
.