0

我使用 nuget 包 "Microsoft.Azure.Storage.Queue" Version="11.1.7" 创建 Azure 存储队列客户端,如下所示,

AsyncLazy<CloudQueue> qClient = new AsyncLazy<CloudQueue>( async () =>
    {
        var myStorageAccount = CloudStorageAccount.Parse("ConnectionString");
        var myQueue = myStorageAccount .CreateCloudQueueClient()
            .GetQueueReference("QueueName");
        await myQueue.CreateIfNotExistsAsync();
        return myQueue;
    });

在通过上面的“qClient”实例将消息发布到队列时,希望结合重试机制来克服任何瞬态故障。

如何在上述创建延迟队列连接的方式中加入重试机制?

4

1 回答 1

0

可以参考这个官方文档,可以使用CloudBlobClient.DefaultRequestOptions属性

我写了一个代码示例,也许它可以启发你:

            AsyncLazy<CloudQueue> qClient = new AsyncLazy<CloudQueue>(async () =>
            {
                var myStorageAccount = CloudStorageAccount.Parse("ConnectionString");
                var myQueue = myStorageAccount.CreateCloudQueueClient();
                myQueue.DefaultRequestOptions = new QueueRequestOptions
                {
                    RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
                    // For Read-access geo-redundant storage, use PrimaryThenSecondary.
                    // Otherwise set this to PrimaryOnly.
                    LocationMode = LocationMode.PrimaryThenSecondary,
                    // Maximum execution time based on the business use case.
                    MaximumExecutionTime = TimeSpan.FromSeconds(20)
                };
                var queue = myQueue.GetQueueReference("QueueName");
                await queue.CreateIfNotExistsAsync();
                return queue;
            });
于 2020-09-25T02:41:36.347 回答