I want to limit no of retry in Azure ServiceBus queue receiver.
Sending messages using console application with MaxRetryCount:3
private static async Task MainAsync()
{
string connectionString = ConfigurationManager.AppSettings["ServiceBusConnection"];
QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, QueueName);
queueClient.RetryPolicy = new RetryExponential(
minBackoff: TimeSpan.FromSeconds(0),
maxBackoff: TimeSpan.FromSeconds(30),
maxRetryCount: 3);
string tradeData = File.ReadAllText("TradeSchemaDemo.json");
var message = new BrokeredMessage(tradeData);
await queueClient.SendAsync(message);
await queueClient.CloseAsync();
}
Another side I have Azure function to receive message,
public static void run([ServiceBusTrigger("TestQueue", AccessRights.Manage, Connection = "servicebusconnection")]string myqueueitem, TraceWriter log)
{
retry++;
System.Console.WriteLine($"Retry attempt {retry}");
throw new System.Exception("Human error");
log.Info($"c# servicebus queue trigger function processed message: {myqueueitem}");
}
Still, my function calling 10 times. Why??
In this case, RetryPolicy
defines the amount of the retries for send operation, not on receiving side.
Receiver retry amount is defined by Queue property Max Delivery Count
. You can set it on queue level with a tool like Service Bus Explorer or programmatically while creating the queue:
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var queue = new QueueDescription(queueName);
queue.MaxDeliveryCount = 3;
if (!namespaceManager.QueueExists(queueName))
namespaceManager.CreateQueue(queue);