簡體   English   中英

ASP.NET調用在異步等待中掛起

[英]ASP.NET call hangs on 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;
}

它不會掛起從測試控制台應用程序調用相同的方法。

當使用相應的同步方法調用時,相同的邏輯也不會掛起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;
}

我應該在哪里尋找缺陷?

缺陷在這里:

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

正如我在博客上解釋的那樣, 您不應阻止異步代碼

代替Result ,使用await

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM