Sojal
Sojal

Reputation: 161

MassTransit - Configure Retry for Azure Service Bus Message Delivery

I am trying to send messages to Azure Service Bus Queue. I am expecting for MassTransit to retry delivery if the connection fails.

My message sender utility:

public class MessageSender : IMessageSender
{
    private readonly IServiceBus _serviceBus;
    
    public MessageSender(IServiceBus serviceBus)
    {
        _serviceBus = serviceBus;
    }

    public async Task SendMessageAsync(IMessage message)
    {
        await _serviceBus.Send(message);
    }
}

I set MassTransit configuration in another class, that is injected in the message sender utility.

public class MassTransitAzureServiceBus : IServiceBus
{
    private IBusControl _busControl;

    public MassTransitAzureServiceBus()
    {
        _busControl = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
            {
                cfg.Host(new Uri(serviceBusUri), h => 
                {
                    h.NamedKeyCredential = new AzureNamedKeyCredential("name","key");

                    h.RetryLimit = 8; // this is not being honored
                });
            });
    }
}

Then I send message like this:

public async Task Send<TMessage>(TMessage message) where TMessage : IMessage
{
     var sendEndpoint = await _busControl.GetSendEndpoint(new Uri("queue-uri"));
     await sendEndpoint.Send(message, typeof(TMessage));
}

I intentionally disconnect internet or provide wrong connection string for service bus as a delivery failure test.

It retries 4 times then throws an error, disregarding the retry limit set by me.

Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry. (No such host is known. (myhost.servicebus.windows.net:443)) (No such host is known. (myhost.servicebus.windows.net:443)) (No such host is known. (myhost.servicebus.windows.net:443)) (No such host is known. (myhost.servicebus.windows.net:443))

I have also tried the following, does not work.

_busControl = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
    cfg.UseMessageRetry(x =>
    {
        x.Immediate(2); // this is not being honored either
    });

    cfg.Host(new Uri(serviceBusUri), h => 
    {
        h.NamedKeyCredential = new AzureNamedKeyCredential("name","key");
    });
});

Is there a way to retry message delivery without using services like Polly? I have used Polly for retrying before, thought I could get rid of it with using MassTransit built-in retry mechanism.

Upvotes: 2

Views: 1114

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33288

First, UseMessageRetry is for consuming messages – it isn't at all related to sending or publishing messages.

Second, MassTransit will retry any of the exception types shown in that retry configuration exponentially up to 1000 times, including publish/send.

Third, you aren't starting the bus. The bus should be created, at startup, and started. When the process shuts down, the bus should be stopped.

Upvotes: 2

Related Questions