Closed agerchev closed 4 years ago
My guess is that Rebus is not properly disposed, when the service provider container is disposed, as it looks like your Rebus instance is still running and processing messages, even though the hosting container is disposed.
Do you know if the service provider container can automatically dispose the disposables in it?
I am using the default Ms container. I think that it is disposing the created objects. I saw in the rebus implementation that the workers are stoped when the bus instance is disposed. If the workers are running how can the pending messages be processed when we are disposing the container(to start the disposing of the bus) If the container is in the process of disposing it cannot serve new requests, I think. Am I wrong ?
@mookid8000 any comment ?
@agerchev I'm so sorry! I've completely missed that you wrote in here.
Have you figured something out?
As you correctly noted, this very much looks like an issue that comes from the fact that the service collection refuses to serve any more requests, once the call to Dispose
has begun... and then, since Rebus might have just taken a message out of its input queue, it might need to resolve some message handlers, before it can shut down.
I think the solution to this problem could be to hook Rebus up somehow with the lifetime events of the generic host. Do you know if this would be a good way forward?
@mookid8000 we have created a simple hosted Service, that do the magic with starting and properly stopping the bus. We included it here (for now): https://github.com/agerchev/Rebus.Oracle/blob/master/Rebus.Oracle/ServiceProvider/RebusHostingService.cs
We do not call UseRebus anymore. If you decide to include this or another version, please tell me, so we can remove our fix :)
Rebus.ServiceProvider.NetCoreServiceProviderContainerAdapter
@agerchev Could you try using the v5 beta version of this package? The changes I made back in February should result in rebus respecting the lifetime of the container a bit better.
As an experiment, I've tried hooking Rebus up with IApplicationLifetime
, if it's present in the container.
This way Rebus will stop when the application is stopping, hopefully avoiding trying to resolve any more message handlers, while the container is being disposed.
It's available in Rebus.ServiceProvider 5.0.0-b04, which is on Nuget.org now 🙂
I am using Rebus using HostBuilder like this:
When we try to close the application when pumping and processing messages an exception is thrown in the processing pipeline:
The problem seems to be in the process of disposing the container and disposing the bus. If i explicitly dispose the bus everything is normal.