Configuring NSB in .Net8 using generic host model and web hosting model

I am working on a .NET 8 application that involves setting up NServiceBus. In our setup, we need to pass an instance of EndpointConfiguration to the builder.UseNServiceBus method. We have a class that returns this EndpointConfiguration instance, but it depends on several other classes, each handling specific functionalities such as:

  • RMQ client connection authentication
  • Reading configuration settings specific to the endpoint from appsettings.json
  • Logic to resolve a connection string for a multi-tenant messaging service

To inject all necessary dependencies into the class responsible for creating the ‘EndpointConfiguration’ instance, I need to resolve the service provider once before calling UseNServiceBus(EndpointConfiguration). This results in two separate service providers.

I am curious to know how other teams are handling this scenario in .NET 8. Specifically, what are the best practices for setting up and configuring EndpointConfiguration with multiple dependencies without ending up with multiple service providers? Any insights or examples would be greatly appreciated.

Thank you!

Hi @syaragat,

Is this a duplicate of Resolve dependencies in the MultiTenantConnectionBuilder method - #4 by mauroservienti?

Hi @tmasternak, This is not a duplicate.

I am currently working on an application that utilizes dependency injection (DI) extensively, where various classes depend on each other to perform specific functionalities. These dependencies are registered in the DI container, and I resolve them through a service provider.

In particular, I am handling the creation of the EndpointConfiguration instance for NServiceBus (NSB), which can be delegated to a dedicated class. This class, in turn, might have its own dependencies. According to the NSB documentation, to register all necessary dependencies for NSB, we need to invoke Builder.UseNServiceBus, passing an EndpointConfiguration instance.

Given this scenario, I need to resolve the EndpointConfiguration instance, which requires building the IServiceCollection. When builder.Build() is called, .NET Core creates a service provider shared by both the framework and the NSB pipeline.

var builder = WebApplication.CreateBuilder(options);
builder.Services.AddTransient<IMyService, MyService>();
builder.Services.AddTransient<IMyDependency, MyDependency>();
builder.Services.AddTransient<IEndpointConfigurationProvider, EndpointConfigurationProvider>();

var serviceProvider = builder.Services.BuildServiceProvider(); // First Service Provider

var endpointConfigurationProvider = serviceProvider.GetRequiredService<IEndpointConfigurationProvider>();
var endpointConfiguration = endpointConfigurationProvider.CreateEndpointConfiguration();

builder.UseNServiceBus(endpointConfiguration);

var app = builder.Build(); // Second Service Provider
app.Run();

My question is: Is it possible to create the service provider only once and use the same instance to resolve the EndpointConfiguration and register all NSB dependencies? This would streamline the process and ensure consistency across the application.

I would appreciate your insights on the best approach to achieve this.

Thank you for your time and expertise.

Hi @syaragat, thank you for the detailed description of your scenario.

Unfortunately, having two service provider instances is currently the only way to access DI during EndpointConfiguration instance creation. I assume this is exactly your case i.e. you require DI (e.g. using Microsoft configuration providers) to set up options on EndpointConfiguation.

We realize this is a suboptimal solution and have this on our radar but will probably take us some time to get there as improving this will require a tighter integration between Microsoft hosting abstractions and the NServiceBus core library.

Cheers,
Tomek

Thank you @tmasternak for your response and for acknowledging the challenge. I appreciate your efforts and look forward to the improvements.