Should I reuse BusControl when connection fails von Start

64 views
Skip to first unread message

Jens Hofmann

unread,
Nov 30, 2017, 5:44:05 AM11/30/17
to masstransit-discuss
Hi there,

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.

Chris Patterson

unread,
Dec 2, 2017, 8:30:04 PM12/2/17
to masstrans...@googlegroups.com
You should NOT reuse the IBusControl returned. If the bus fails to start, or is stopped, you should create it again.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/969a5e83-9f6c-464e-b11e-fd4dd0ac0522%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages