Microsoft.Extensions.DependencyInjection

Source
NuGet Package NServiceBus.MSDependencyInjection (1-pre) | License
This is a community maintained project. License and support are independent of Particular Software.
This page targets a pre-release version and is subject to change prior to the final release.

NServiceBus can be configured to use Microsoft.Extensions.DependencyInjection for dependency injection.

Usage

public void ConfigureServices(IServiceCollection services)
{
    // register any services to IServiceCollection

    endpointConfiguration.UseContainer<ServicesBuilder>(customizations =>
    {
        customizations.ExistingServices(services);
    });
}

If there is the need to share the DI container between NServiceBus and the hosting .NET Core application the following code can be used:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // register any services to IServiceCollection
    
    UpdateableServiceProvider container = null;
    endpointConfiguration.UseContainer<ServicesBuilder>(customizations =>
    {
        customizations.ExistingServices(services);
        customizations.ServiceProviderFactory(sc => 
        {
            container = new UpdateableServiceProvider(sc);
            return container;
        });
    });

    endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

    return container;
}

DependencyLifecycle mapping

DependencyLifecycle maps to Microsoft.Extensions.DependencyInjection service lifetimes as follows:

DependencyLifecycleMicrosoft.Extensions.DependencyInjection service lifetime
InstancePerCallTransient
InstancePerUnitOfWorkScoped
SingleInstanceSingleton

Samples


Last modified