简体   繁体   English

ASP.NET调用在异步等待中挂起

[英]ASP.NET call hangs on async await

My calls to a Google API from an API Controller in IIS express hang indefinitely when called sequentially using async await. 当使用async await顺序调用时,我从IIS中的API控制器对Google API的调用会无限期地挂起。

var id = CreateDocument("My Title").Result;

async Task<string> CreateDocument(string title)
{
    var file = new GData.File { Title = title };
    // Stepping over this line in the debugger never returns in IIS Express.
    file = await Service.Files.Insert(file).ExecuteAsync();
    return file.Id;
}

It does not hang calling the same method from a test console app. 它不会挂起从测试控制台应用程序调用相同的方法。

The same logic also does not hang IIS Express when called using the corresponding synchronous method instead. 当使用相应的同步方法调用时,相同的逻辑也不会挂起IIS Express。

var id = CreateDocument("My Title");

string CreateDocument(string title)
{
    var file = new GData.File { Title = title };
    // This has no problem
    file = Service.Files.Insert(file).Execute();
    return file.Id;
}

Where should I look for the defect? 我应该在哪里寻找缺陷?

The defect is here: 缺陷在这里:

var id = CreateDocument("My Title").Result;

As I explain on my blog, you should not block on async code . 正如我在博客上解释的那样, 您不应阻止异步代码

Instead of Result , use await : 代替Result ,使用await

var id = await CreateDocument("My Title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM