Azure downloadtostreamasync method hangs

前端 未结 2 994
鱼传尺愫
鱼传尺愫 2021-01-11 10:55

here is the offending code

    public async static Task AsyncReadBlob(string identifier)
    {
        CloudStorageAccount storageAccount         


        
相关标签:
2条回答
  • 2021-01-11 11:39

    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.

    0 讨论(0)
  • 2021-01-11 11:52

    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.

    0 讨论(0)
提交回复
热议问题