I'm trying to upgrade a solution from NServiceBus 5 to 6. Currently, we are injecting IBus to a service class so that the service can send messages to the bus as needed. Now that IBus is no longer available in 6, how do I inject the endpoint instance when the endpoint is created using NServiceBus.Host, which will itself start this endpoint?
There is an example provided at the bottom of https://docs.particular.net/samples/hosting/multi-hosting/:
var endpointConfiguration = new EndpointConfiguration("Samples.MultiHosting.Instance1");
endpointConfiguration.UseSerialization<JsonSerializer>();
endpointConfiguration.EnableInstallers();
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.SendFailedMessagesTo("error");
return Endpoint.Start(endpointConfiguration);
However, it is immediately followed by a note indicating "this is possible only when self-hosting and not using NServiceBus.Host."
Use an IWantToRunWhenEndpointStartsAndStops
implementation to inject the IMessageSession
for your service class after the bus has started.
Here is the relevant section from the Upgrade Guide for NServiceBus.Host: https://docs.particular.net/nservicebus/upgrades/host-6to7#iwanttorunwhenendpointstartsandstops-interface-in-version-7-of-nservicebus-host
An alternative would be to switch to the NServiceBus Windows Service Bootstrap instead of using NServiceBus.Host where you will have more control over the endpoint's lifecycle.