Search code examples
c#.net-corerabbitmqmicroservicesmasstransit

MassTransit trying to connect to RabbitMQ infinitely when it is down


When RabbitMQ is down, by default (I think) Masstransit trying to connect infinitely and getting "RabbitMQ Connect Failed: Broker unreachable: guest@localhost:5672/" error.

Masstransit started working as soon as RabbitMQ is up. Is there anyway we can set a limit on the number of retries? So Masstransit will stop retrying to connect RabbitMQ.


Solution

  • To limit the time spent connecting to RabbitMQ, pass a CancellationToken to the StartAsync method.

    using var source = new CancellationTokenSource(TimeSpan.FromSeconds(30));
    
    await bus.StartAsync(source.Token);