Azure存储队列-重试机制实现

Azure storage queue - retry mechanism implementation

我正在使用 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' 实例将消息发布到队列时合并重试机制以克服任何瞬态故障。

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

可以参考这个official document, and you can use 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;
            });