ASP.NET 调用挂起异步等待

sha*_*non 2 asp.net iis-express async-await

当使用异步等待顺序调用时,我从 IIS express 中的 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;
}
Run Code Online (Sandbox Code Playgroud)

它不会挂起从测试控制台应用程序调用相同的方法。

当使用相应的同步方法调用时,相同的逻辑也不会挂起 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;
}
Run Code Online (Sandbox Code Playgroud)

我应该在哪里寻找缺陷?

Ste*_*ary 5

缺陷在这里:

var id = CreateDocument("My Title").Result;
Run Code Online (Sandbox Code Playgroud)

正如我在我的博客中所解释的,你不应该阻塞异步代码

而不是Result,使用await

var id = await CreateDocument("My Title");
Run Code Online (Sandbox Code Playgroud)