gpt4 book ai didi

ASP.NET 调用因异步等待而挂起

转载 作者:行者123 更新时间:2023-12-02 17:19:11 24 4
gpt4 key购买 nike

当使用异步等待顺序调用时,我从 IIS Express 中的 API Controller 对 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;

正如我在博客中所解释的,you should not block on async code .

使用await而不是Result:

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

关于ASP.NET 调用因异步等待而挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30668258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com