Reconfiguring NServiceBus Loses Messages
This is my first post on StackOverflow.
I am trying to put NServiceBus behind an interface that looks like the following:
interface IMessagingService
{
IObserver<T> RegisterPublication<T>() where T : IPublishedMessage;
IObservable<T> RegisterSubscription<T>() where T : IPublishedMessage;
}
In order to support this interface without forcing users to have an explictit Start() call, I need to reconfigure the bus each time RegisterSubscription is invoked with a call to a method that looks like this:
IBus ConfigureBus()
{
Configure config = BuildConfiguration();
return config.CreateBus().Start();
}
I can't copy and paste the real code, but hopefully you can see that I need to have a started bus to listen for messages once RegisterSubscription is invoked. I need to add subscribers on subsequent invocations.
The problem that I am running into is that each call to ConfigureBus() seems to be spinning up more开发者_开发知识库 threads and results in messages being lost. My handler instances are not invoked, yet NServiceBus logs that it finished handling each of the messages. With four calls to RegisterSubscription, I lose about 40% of my messages.
Is there a safe way to reconfigure and restart the IBus on the fly without causing this problem? The IBus.Subscribe doesn't seem to work.
@bsf's answer
I found a solution. CreateBus()
returns an IStarableBus
. If I keep a static reference to that and dispose it before my next call to CreateBus()
, everything seems to work fine.
精彩评论