Azure downloadtostreamasync method hangs

笑着哭i 提交于 2019-12-19 05:21:24

问题


here is the offending code

    public async static Task<MemoryStream> AsyncReadBlob(string identifier)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        MemoryStream memstream = new MemoryStream();
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(identifier);
        await blockBlob.DownloadToStreamAsync(memstream);
        return memstream;
    }

Basically, the code runs completely fine if I replace

await blockBlob.DownloadToStreamAsync(memstream)

with

blockblob.DownloadToStream(memstream)

or

blockblob.DownloadToStreamAsync(memstream).wait()

But the moment I make it asynchronous, the "download to stream" step never completes. I've also tried using DownloadToStreamBegin() and then waiting for it to complete too, but it never finishes, once again.

Is it not working because it's a static method? Or is it because it's memstream is being de-referenced from memory or something??

I'm using asp.net 4.5, all compiles properly, no interpreter errors or exceptions thrown.

Any help would be greatly appreciated


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/28526249/azure-downloadtostreamasync-method-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!