here is the offending code
public async static Task AsyncReadBlob(string identifier)
{
CloudStorageAccount storageAccount
Short version... Use:
await blockBlob.DownloadToStreamAsync(memstream).ConfigureAwait(false);
Long version:
When you await a task in C#, the task scheduler will try to execute the block of code that comes after the await in the same thread that called the original await.
The problem is that in some cases (can't recall right now the specific circumstances) the ASP.NET Thread Scheduler will block the thread while waiting for the Task to return. When the task returns, the task blocks waiting for the original thread to be released, while the thread is blocked waiting for the task to end. So you end deadlocked.
ConfigureAwait allows you to deactivate that behavior.
Your code almost certainly is calling Task<T>.Result
or Task.Wait
further up the call stack from AsyncReadBlob
, and it is this blocking code that is causing the deadlock. I describe this scenario in more detail on my blog.
The correct solution is to replace all Result
and Wait
calls with await
.