Specify Publish timeouts in mass transit

Is there a way to specify timeout value when publishing messages using the MassTransit Library. How to handle scenarios when message broker goes down. Now it seems that Publish call waits indefinitely. It would be nice to control these behavior. Should we rely on cancellation token? Timeout implementation might be better.

1 answer

  • answered 2020-10-27 20:09 Chris Patterson

    You can pass a CancellationToken to Publish. If canceled, an OperationCanceledException will be thrown. If you want to use a timeout, you can create a CancellationTokenSource with a timeout.

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