c# - 异步发布到 Azure 队列

标签 c# async-await azure-queues

我尝试像这样在 Azure 队列中异步排队消息:

private async Task EnqueueItemsAsync(IEnumerable<string> messages) {
            var tasks = messages.Select(msg => _queue.AddMessageAsync(new CloudQueueMessage(msg),
                null, null, null, null));

            await Task.WhenAll(tasks);
        }

如果我做对了,它会说“开始一个接一个地排队,而不是等待它们被发布,为每个任务保留一个引用,然后等到所有被发布”。

此代码在大多数情况下工作正常,但对于大量项目(5000),它开始排队然后抛出超时异常(在排队约 3500 个项目之后)。

我通过等待每个完成后再继续下一个来解决这个问题

private async Task EnqueueItemsAsync(IEnumerable<string> messages) {
            foreach (var message in messages) {
                await _queue.AddMessageAsync(new CloudQueueMessage(message), null, null, null, null);
            }
        }

谁能解释为什么会这样?

异常(exception):

System.AggregateException which wraps many such exceptions: Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass4.<CreateCallbackVoid>b__3(IAsyncResult ar) Request Information RequestID: RequestDate: StatusMessage: <--- ---> (Inner Exception #1) Microsoft.WindowsAzure.Storage.StorageException: The client could not finish the operation within specified timeout. ---> System.TimeoutException: The client could not finish the operation within specified timeout. --- End of inner exception stack trace --- Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result)`.

最佳答案

Azure 中的队列设计吞吐量为每秒 2000 条消息。

参见:Azure Storage Scalability and Performance Targets

When your application reaches the limit of what a partition can handle for your workload, Azure Storage will begin to return error code 503 (Server Busy) or error code 500 (Operation Timeout) responses. When this occurs, the application should use an exponential backoff policy for retries. The exponential backoff allows the load on the partition to decrease, and to ease out spikes in traffic to that partition.

关于c# - 异步发布到 Azure 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608373/

相关文章:

c# - 在 json.net 中转换为 DateTime

c# - 如何在asp.net mvc中验证PayPal企业电子邮件是否正确

reactjs - 我如何将嵌套的 promise 传递给异步等待

c# - 有没有办法动态确定启动时在 Azure webjob 中触发的队列数量和名称?

c# - 从 Android 模拟器连接到本地主机上的 wcf 服务时遇到问题

c# - 未找到 SystemMediaTransportControls

javascript - 异步/等待 : do async nested functions automatically need to be awaited a level above?

c# - 为什么我不能将异步代码作为同步运行

python - 如何处理Python Azure存储SDK中的异常

azure - 如何判断 Azure 队列中的最后一条消息何时被删除