Hi,
I’m having trouble determining what exactly needs to change in my project in response to this https://particular.net/blog/nservicebus-now-supports-microsoft-extensions-dependencyinjection:
Looking at your sample downloaded from here:
the pertinent section of new code looks like this:
var containerSettings = endpointConfiguration.UseContainer(new DefaultServiceProviderFactory());
containerSettings.ServiceCollection.AddSingleton(new MyService());
The analogous section of our existing NSB configuration is using the following:
endpointConfiguration.UseContainer<ServicesBuilder>(customizations =>
{
customizations.ExistingServices(services);
customizations.ServiceProviderFactory(sc =>
{
var container = new UpdateableServiceProvider(sc);
return container;
});
});
where ServicesBuilder comes from NServiceBus namespace,
and “services” refers to an IServiceCollection that we’ve already built up out of a lot of other code external to our “ConfigureNServiceBus” logic that is passed into it.
With the existing code, we’re already able to inject whatever classes we need to into our Handlers using standard constructor injection.
So I’m trying to figure out
a) whether I need to change and
b) if I do, how can I make this change without having to move all of my configuration into the NServiceBus configuration section so I can manually add each dependency with proper scope to this new container?
I can’t really just do a “foreach” adding every service from ‘services’ into the containerSettings using AddSingleton because each of these services was originally configured with the proper scope, as they aren’t all Singletons, so just trying to port them from services could be messy, non maintainable code.
Worst case scenario I guess I’d just have to mix all the configuration together but making my config less clean doesn’t seem like the right solution, so wanted to get your take. I could also potentially break up our NSB config into two parts, creating this container first before doing any other service config, then passing it into all the other config methods to add their services to it, then doing the final NSB config that depends on other previous activities.
But, before I go through the effort I want to understand if the code we’re using is actually part of what is deprecating because that wasn’t super clear from the documentation or examples.
Thanks in advance,
Anye M. Shafer