All the way reading your reply I keep thinking “but why can’t you simply just leave everything registered/resolved and just expose IEndpointInstance so I can call Start/Stop on it that would just pause its internal message pump loop without going through all that dependency injection nightmarish scenario?”
Now I realized that even in my old self-hosting code, I would create a new DI container and configuration on every re-start because RunningEndpoint can only be stopped, it does not have a Start method. There’s probably a reason why you designed the system in a way that endpoint startup can’t be decoupled from its creation, and its creation can’t be decoupled from the DI registration, but this goes outside my field of expertise.
So I think it is probably simpler to move the endpoint that needs to be started and stop into it’s own service and then start and stop that one on demand as needed.
Do you have a quick sample that would show me how to do that? Because as you mentioned, this seems impossible to work around with the way MS DI works.
To be clear, are you suggesting to forget about using the NServiceBus.Extension.Hosting
nuget and UseNServiceBus()
altogether and instead control the endpoint lifetime in one of my service? Maybe by using NServiceBus.Extensions.DependencyInjection
and a EndpointWithExternallyManagedServiceProvider
? If you remember, I had quite the issues trying to use it previously (message pump would start before DI resolving would finish) and put it on hold until I had the budget to move my solutions to Net5 and use NServiceBus.Extension.Hosting
which I hoped would help get over it since you put a lot of work into it.
Now I finally got the budget to port csproj, configurations, updating nugets for compatibility and refactoring my services to use IHostedService which keeps my hands full already. I was glad to finally move to a proper structure with MS Host, DI, Logging and such and hoped that this time the NServiceBus transition would be painless, but it’s not the case. I already spent way too many hours trying to make sense of it all, and feel like I’m driving straight into a wall.