Child containers

Component: NServiceBus
NuGet Package NServiceBus (5.x)
Standard support for version 5.x of NServiceBus has expired. For more information see our Support Policy.

Child containers are a snapshot of the main container providing an additional instance lifetime scope.

Child container scope in NServiceBus

NServiceBus creates a child container for each transport message that is received. During message processing, all InstancePerUnitOfWork scoped instances that are created are resolved as singletons within the context of the same message. This is helpful to support the sharing of database sessions and other resources with lifetimes specific to the processed message.

Objects can be configured in a child container scope using the InstancePerUnitOfWork lifetime:

busConfiguration.RegisterComponents(
    registration: components =>
    {
        components.ConfigureComponent<MyUnitOfWork>(DependencyLifecycle.InstancePerUnitOfWork);
    });

Deterministic disposal

Child containers automatically dispose all IDisposable instances created with InstancePerUnitOfWork lifetime once the message is processed. This is useful for managing things like database sessions which must be disposed of properly.


Last modified