Is there any way to tell MassTransit that if it can't reach the broker when calling Bus.Start(), it should treat it as it would a transient disconnect, and just keep trying to reach the broker in the background?
Here the recommendation is to fail to start the application, but I think being able to choose the behavior in that scenario would be beneficial. In my environment, if RabbitMQ is unreachable it's most probably a transient issue, so I'd rather have my application start normally while the bus keeps trying to reach the broker until it succeeds. If messages are published before the bus can connect to the broker, I'm fine handling the individual exceptions (or if they can be queued somehow, even better), but I'd like the application to start anyway.
With MassTransit 4.0.0.1326-develop, this worked for my purpose:
try
{
busHandle = bus.StartAsync().GetAwaiter().GetResult();
}
catch (RabbitMqConnectionException e)
{
if (e.InnerException is BrokerUnreachableException)
{
// A second call will succeed, AND we need it to populate busHandle to be able to stop the bus later
busHandle = bus.StartAsync().GetAwaiter().GetResult();
}
else
{
throw;
}
}
// My application code ...
busHandle.Stop(TimeSpan.FromSeconds(30));
But with version 5.1.3 the second call to bus.StartAsync().GetAwaiter().GetResult() fails with MassTransit.MassTransitException: 'The host was already started: guest@localhost:5672/', from here.
I can find no other way of grabbing a busHandle from the IBusControl ('bus' in the code above), and I imagine that if I create a new instance of something that implements IBusControl (which was obtained from DI), the rest of my code will still be using the one from DI that failed to start...?
So to restate the question, is there any way to have MassTransit treat an unreachable broker during bus.Start() as a transient failure instead of leaving us no choice but to fail to start the application?