Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop HttpClient.GetAsync(uri) from hanging? [duplicate]

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();

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.

like image 947
Piers Karsenbarg Avatar asked Feb 24 '14 12:02

Piers Karsenbarg


Video Answer


1 Answers

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.

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

like image 154
Stephen Cleary Avatar answered Oct 09 '22 19:10

Stephen Cleary