如何从吊停止HttpClient.GetAsync(URI)? [英] How to stop HttpClient.GetAsync(uri) from hanging?

查看:205
本文介绍了如何从吊停止HttpClient.GetAsync(URI)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用调用一个URI以下code:

I have the following code that I'm using to call a uri:

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

URI是在自定义的Web API的URI指向(我可以调试和跟进)。然而,当在API的最后一个return语句被调用,好像没有什么改变。它应该去 response.EnsureSuccessStatus code(); 线,但事实并非如此。它只是似乎挂。

The uri is pointing at a custom web api uri (which I can debug and follow through). However, when the last return statement in the api has been called, nothing seems to happen. It should go to the response.EnsureSuccessStatusCode(); line, but it's not. It just seems to be hanging.

推荐答案

我的蜘蛛侠,感觉告诉我,进一步您的通话堆栈的code是返回的任务阻塞,如任务.Wait 任务< T>。结果。默认情况下,这将导致死锁,我在我的博客解释。

My spidey-sense tells me that further up your call stack your code is blocking on a returned task, e.g., Task.Wait or Task<T>.Result. By default, this will cause a deadlock that I explain on my blog.

要解决这个问题,替换 Task.Wait 任务&LT; T&GT;。结果等待

To fix it, replace Task.Wait and Task<T>.Result with await.

这篇关于如何从吊停止HttpClient.GetAsync(URI)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆