Will Azure Service Bus continue retry after the Send call is complete with a fault/exception?

The following is the code for sending batched messages and the code block following is the setting for retry policy while creating the service bus client.
While testing a scenario involving transient network fault, we see the log that the send task was faulted but the messages were seen in the service bus for the message-id that were assigned to the messages of the batch. Is it possible that the retry kept happening in the background even after entering the fault? I am of the understanding that the fault will happen only AFTER the retry procedures are completed by the SDK. Is that correct?

Secondary query – How do I log the internal retry attempts?

//Code for sending. Uses Azure.Messaging.ServiceBus
 await tempSender.SendMessagesAsync(batch).ContinueWith(async (tStat) =>
                    {
                        if (tStat.IsFaulted || tStat.IsCanceled)
                        {
                            _logger.Log($"Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
                        }
                        else
                        {
                            _logger.Log($"Sent batch of size {batch.Count}", EventLevel.LogAlways);
                            onCompletionCallBack(messages);

                        }
                    });
//Retry policy
protected ServiceBusRetryOptions GetRetryPolicy() => new ServiceBusRetryOptions()
        {
            Delay = TimeSpan.FromSeconds(0.1),
            MaxDelay = TimeSpan.FromSeconds(30),
            MaxRetries = 5,
            Mode = ServiceBusRetryMode.Exponential,
        };

>Solution :

Your code could be simplified to the following:

try
{
  await tempSender.SendMessagesAsync(batch);

  _logger.Log($"Sent batch of size {batch.Count}", EventLevel.LogAlways);

  onCompletionCallBack(messages);
}
catch (Exception exception)
{
  _logger.Log($"Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
}

Service Bus SDK will not throw an exception until all retries are exhausted. There’s a chance that the messages have been dispatched, but the acknowledgement hasn’t been received, which will explain what you’re observing.

Leave a Reply