Reputation: 1577
With no luck, I tried configuring my ServiceBusClient to retry a message with a Fixed Delay of 10 seconds. I also tried Exponential retries configuration. However, the code is always retrying the message within a second or 2 and completely ignoring the configuration. It even ignores the MaxRetries and only retries 10 times, the value configured in Azure Portal for the queue. What am I doing wrong?
I am using The Azure.Messaging.ServiceBus library, NuGet package 7.0.0.
The code:
ServiceBusClient client = new ServiceBusClient(serviceBusConnectionString, new ServiceBusClientOptions()
{
RetryOptions = new ServiceBusRetryOptions()
{
Mode = ServiceBusRetryMode.Fixed,
Delay = TimeSpan.FromSeconds(10),
MaxDelay = TimeSpan.FromMinutes(3),
MaxRetries = 30
}
});
ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());
// throwing an exception in MyMessageHandlerAsync on purpose
// to test out the retries configuration
processor.ProcessMessageAsync += MyMessageHandlerAsync;
// The uncaught exception causes this method to execute.
// Processing is attempted 10 times with
// virtually no delay between each attempt.
// After the 10th attempt, the message goes to deadletter,
// which is expected.
processor.ProcessErrorAsync += MyErrorHandler;
I'm adding more to this question after receiving the 1st response:
Currently, MyMessageHandlerAsync is:
private async Task MyMessageHandlerAsync(EventArgs eventArgs)
{
var args = (ProcessMessageEventArgs)eventArgs;
var body = args.Message.Body.ToString();
// ...
// process body
// ...
await args.CompleteMessageAsync(args.Message);
}
How should I change the method's contents to retry a non-transient ServiceBusException? Please help provide the code where the TODOs are below:
private async Task MyMessageHandlerAsync(EventArgs eventArgs)
{
var args = (ProcessMessageEventArgs)eventArgs;
try
{
var body = args.Message.Body.ToString();
// ...
// process body
// ...
await args.CompleteMessageAsync(args.Message);
}
catch (ServiceBusException sbe)
{
if (sbe.IsTransiet)
{
// TODO: Is it correct that the exponential retry will work
// here? The one defined in the ServiceBusClient.
// So, no code is needed here, just throw.
throw;
}
else
{
// TODO: for non-transient, this is where the
// options in the ServiceBusClient don't apply.
// Is that correct? How do I do an
// exponential retry here?
}
}
catch (Exception e)
{
// TODO: same problem as else in first catch.
}
}
Upvotes: 4
Views: 7976
Reputation: 4870
ServiceBusRetryOptions
is intended to be used by the ASB client when there are transient errors that are not bubbled up to your code right away, i.e. an internal retry mechanism built into the client to perform retries on your behalf before exception is raised.
Use retry policy to specify to the ASB client how to deal with transient errors prior to giving up, not how many times a message handler throws error:
Upvotes: 3