ServiceBus重试策略不与QueueClient一起使用

时间:2018-06-08 06:40:21

标签: azure azure-functions servicebus

我想限制Azure ServiceBus队列接收器中的重试次数。

使用 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();
    }

另一方面,我有Azure功能来接收消息,

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}");
    }

仍然,我的功能调用了10次。为什么?

2 个答案:

答案 0 :(得分:0)

在这种情况下,RetryPolicy定义发送操作的重试次数,而不是接收方。

接收方重试量由队列属性Max Delivery Count定义。您可以使用Service Bus Explorer之类的工具在队列级别上设置它,也可以在创建队列时以编程方式设置它:

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var queue = new QueueDescription(queueName);
queue.MaxDeliveryCount = 3;
if (!namespaceManager.QueueExists(queueName))
    namespaceManager.CreateQueue(queue);

答案 1 :(得分:0)

如果有人想查看代码

private static async Task MainAsync()
    {
        string connectionString = ConfigurationManager.AppSettings["ServiceBusConnection"];
        var nm = NamespaceManager.CreateFromConnectionString(connectionString);
        var queue = new QueueDescription(QueueName);
        queue.MaxDeliveryCount = 3;
        if (!nm.QueueExists(QueueName))
            await nm.CreateQueueAsync(queue);

        QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, QueueName);
        string tradeData = File.ReadAllText("TradeSchemaDemo.json");
        var message = new BrokeredMessage(tradeData);
        await queueClient.SendAsync(message);
        await queueClient.CloseAsync();
    }
相关问题