5

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.

Piers Karsenbarg
  • 3,105
  • 5
  • 31
  • 67

1 Answers1

11

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.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I was just going to suggest trying `await client.GetAsync(uri).ConfigureAwait(false)`, which might eliminate a deadlock like that :) – noseratio Feb 24 '14 at 12:46
  • Yeah I found that fix just after writing the question (in the "related" column on the right hand side) – Piers Karsenbarg Feb 24 '14 at 13:01
  • 2
    `ConfigureAwait` is more of a workaround than a solution, IMO; you end up with more brittle code. The more proper solution is to use `await` instead of `Wait` or `Result`. – Stephen Cleary Feb 24 '14 at 13:06