I try to enqueue messages in Azure Queues asynchronously like this:
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);
}
If I get it right this says "start enqueuing one item after the other without waiting them to get posted, keep a reference for each task and then wait until all get posted".
This code works fine in most cases, but for a large number of items (5000), it starts enqueuing and then throws a timeout exception (after having enqueued ~3500 items).
I solved it by waiting each one to finish before continuing with the next one
private async Task EnqueueItemsAsync(IEnumerable<string> messages) {
foreach (var message in messages) {
await _queue.AddMessageAsync(new CloudQueueMessage(message), null, null, null, null);
}
}
Can anyone explain why this happened?
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)`.