Reputation: 11
We are using NServiceBus version 7.5 and Windsor Container. We load dependencies to a Windsor container. The dependencies are (for example) from loading FromAssemblyContaining. We load existing Windsor container like that:
endpointConfiguration.UseContainer<WindsorBuilder>(c => c.ExistingContainer(container));.
Everything works quite well. But now we need to migrate NServiceBus to version 8. Where UseContainer
is not supported any more. In NServiceBus 8, we have to load dependencies through AddSingleton
, AddScoped
or AddTransient
.
How do I load all necessary dependencies (the existing container). Listing all Services in the old code (load from FromAssemblyContaining
) is impossible.
Thank you very much!
I tried something as here https://docs.particular.net/nservicebus/upgrades/7to8/dependency-injection:
var endpointConfiguration = new EndpointConfiguration("EndpointName");
_endpointCustomization.Customize(endpointConfiguration); //setup things for endpoint
var serviceCollection = new ServiceCollection();
var container = _endpointCustomization.Container() // my function to load existing Windsor container
var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(container, serviceCollection);
var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection);
var _endpointInstance = await startableEndpoint.Start(serviceProvider);
await _endpointCustomization.Start(_endpointInstance);
It throws exception at the first await call "No component for supporting the service NServiceBus.Unicast.Messages.MessageMetadataRegistry was found" Castle.MicroKernel.ComponentNotFoundException
Upvotes: 0
Views: 280
Reputation: 789
Your approach is finalizing the Windsor container already before calling EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection);
. Create
will add NServiceBus specific registration to the ServiceCollection which will then not be reflected to the service provider instance that you already created. Try the following instead:
var endpointConfiguration = new EndpointConfiguration("EndpointName");
_endpointCustomization.Customize(endpointConfiguration); //setup things for endpoint
var serviceCollection = new ServiceCollection();
var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection);
var container = _endpointCustomization.Container() // my function to load existing Windsor container
var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(container, serviceCollection);
var _endpointInstance = await startableEndpoint.Start(serviceProvider);
await _endpointCustomization.Start(_endpointInstance);
Since I don't know what your _endpointCustomization
instance is doing exactly, I don't know whether that will solve the issues but this suggestion represents the order the endpoint should be configured (also documented here: https://docs.particular.net/nservicebus/upgrades/7to8/dependency-injection#externally-managed-container-mode-migrating-to-externally-managed-mode-castle-windsor)
Upvotes: 1