Support for the Microsoft.Extensions.DependencyInjection container.
public void ConfigureServices(IServiceCollection services)
{
// register any services to IServiceCollection
// ...
var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseTransport<LearningTransport>();
endpointConfiguration.UseContainer<ServicesBuilder>(customizations =>
{
customizations.ExistingServices(services);
});
endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
}
To share the used service provide across (ASP) .NET Core and NServiceBus (recommended) you need to return the created service provider.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// register any services to IServiceCollection
// ...
var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseTransport<LearningTransport>();
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;
}
If you need to register the IEndpointInstance
also to the service collection please use following pattern:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
IEndpointInstance endpointInstance = null;
services.AddSingleton<IMessageSession>(sp => endpointInstance);
endpointConfiguration.UseContainer<ServicesBuilder>(c => c.ExistingServices(services));
endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
}