Azure downloadtostreamasync方法挂起 [英] Azure downloadtostreamasync method hangs

查看:115
本文介绍了Azure downloadtostreamasync方法挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有问题的代码

    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)

使用

blockblob.DownloadToStream(memstream)

blockblob.DownloadToStreamAsync(memstream).wait()

但是,当我使其异步时,下载到流"步骤将永远不会完成.我也尝试过使用DownloadToStreamBegin(),然后也等待它完成,但是它永远不会完成.

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??

我使用的是asp.net 4.5,所有编译均正确,没有解释器错误或异常.

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

任何帮助将不胜感激

推荐答案

简短版本...使用:

 await blockBlob.DownloadToStreamAsync(memstream).ConfigureAwait(false);

长版:

当您使用C#等待任务时,任务计划程序将尝试在调用原始等待的同一线程中执行等待之后的代码块.

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.

问题是,在某些情况下(现在无法回忆起特定情况),ASP.NET线程计划程序将在等待任务返回时阻塞线程.当任务返回时,任务将阻塞以等待原始线程被释放,而线程被阻塞以等待任务结束.这样您就陷入僵局了.

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 使您可以停用该行为.

ConfigureAwait allows you to deactivate that behavior.

这篇关于Azure downloadtostreamasync方法挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆