I have the following code in my ConfigureServices method (
asp.net core 2, masstransit 4) with retry-logic (5-times) to start the bus and establish connection to rabbitmq.
var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
{
var host = sbc.Host(new Uri("rabbitmq://rabbitmq"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
bool connectionEstablished = false;
int count = 0;
do
{
try
{
bus.Start();
connectionEstablished = true;
_logger.LogInformation("Connection to messagebus established");
}
catch (RabbitMqConnectionException exception)
{
if (count == 5)
{
_logger.LogError(exception, "Exception on connecting to messagebus ");
throw new InvalidOperationException("Failed to connect to messagebus ");
}
connectionEstablished = false;
count++;
_logger.LogWarning("Failed to connect to messagebus . Try again");
System.Threading.Thread.Sleep(200);
}
} while (!connectionEstablished);
My problem now is that the first call to bus.Start() throws the excepted RabbitMqConnectionException, but the second call does not. So is it not save to call Start(), in case of an exception, multiple times. Is it a Bug or should I use a fresh instance from factory each time.