Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何阻止HttpClient.GetAsync(uri)挂起?_C#_Asp.net Web Api_Async Await - Fatal编程技术网

C# 如何阻止HttpClient.GetAsync(uri)挂起?

C# 如何阻止HttpClient.GetAsync(uri)挂起?,c#,asp.net-web-api,async-await,C#,Asp.net Web Api,Async Await,我使用以下代码调用uri: var uri = string.Format("Helpers/ProfileList/Online/?locationId=0&skip=0&take={0}", numProfiles); HttpResponseMessage response = await client.GetAsync(uri); response.EnsureSuccessStatusCode(); uri指向一个定制的WebAPI uri(我可以调试并遵循它)。然而

我使用以下代码调用uri:

var uri = string.Format("Helpers/ProfileList/Online/?locationId=0&skip=0&take={0}", numProfiles);
HttpResponseMessage response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();

uri指向一个定制的WebAPI uri(我可以调试并遵循它)。然而,当调用api中的最后一个return语句时,似乎什么也没有发生。它应该转到
响应。EnsureSuccessStatusCode()行,但它不是。它似乎是挂起的。

我的蜘蛛感觉告诉我,在调用堆栈的更上层,您的代码正在阻止返回的任务,例如,
任务。等待
任务。结果
。默认情况下,这将是我在博客上解释的内容


要修复它,请将
Task.Wait
Task.Result
替换为
await

我只是建议尝试
await client.GetAsync(uri.ConfigureAwait(false)
,这可能会消除这样的死锁:)是的,我在写完问题后(在右侧的“相关”列中)就找到了修复方法
ConfigureAwait
与其说是一个解决方案,不如说是一个解决方案,IMO;你最终会得到更脆弱的代码。更合适的解决方案是使用
Wait
而不是
Wait
Result