Async call with await in HttpClient never returns

await httpclient.postasync never returns
async/await deadlock
async/await thread pool
c# httpclient sendasync does not return
httpclient deadlock
async/await c#
c# async/await thread safety
c# async/await thread pool

I have a call I am making from inside a xaml-based, C# metro application on the Win8 CP; this call simply hits a web service and returns JSON data.

HttpMessageHandler handler = new HttpClientHandler();

HttpClient httpClient = new HttpClient(handler);
httpClient.BaseAddress = new Uri("http://192.168.1.101/api/");

var result = await httpClient.GetStreamAsync("weeklyplan");
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WeeklyPlanData[]));
return (WeeklyPlanData[])ser.ReadObject(result);

It hangs at the await but the http call actually returns almost immediately (confirmed through fiddler); it is as if the await is ignored and it just hangs there.

Before you ask - YES - the Private Network capability is turned on.

Any ideas why this would hang?

Check out this answer to my question which seems to be very similar.

Something to try: call ConfigureAwait(false) on the Task returned by GetStreamAsync(). E.g.

var result = await httpClient.GetStreamAsync("weeklyplan")
                             .ConfigureAwait(continueOnCapturedContext:false);

Whether or not this is useful depends on how your code above is being called - in my case calling the async method using Task.GetAwaiter().GetResult() caused the code to hang.

This is because GetResult() blocks the current thread until the Task completes. When the task does complete it attempts to re-enter the thread context in which it was started but cannot because there is already a thread in that context, which is blocked by the call to GetResult()... deadlock!

This MSDN post goes into a bit of detail on how .NET synchronizes parallel threads - and the answer given to my own question gives some best practices.

"await HTTPClient.GetAsync" never completing, GetAsync(…) never returns when using await/async (6 answers) ConfigureAwait(false) with each of your await . await GetAPIAsync();. Do: Async call with await in HttpClient never returns (1) . I have a call I am making from inside a xaml-based, C# metro application on the Win8 CP; this call simply hits a web service and returns JSON data.

Just a heads up - if you miss the await at the top level in an ASP.NET controller, and you return the task instead of the result as a response, it actually just hangs in the nested await call(s) with no errors. A silly mistake, but had I seen this post it might have saved me some time checking through the code for something odd.

HttpClient GetAsync never returns, HttpClient GetAsync never returns. VictorNguyen1902 The calling method is not async/await so I thought maybe it was being swallowed. You are executing an asynchronous method. This means that any exceptions will be thrown when you call await on the returned task or when you try to retrieve the results using await myTask; You never do so, which means that the exception is thrown and caught higher up your call

Disclaimer: I don't like the ConfigureAwait() solution because I find it non-intuitive and hard to remember. Instead I came to the conclusion to wrap non awaited method calls in Task.Run(() => myAsyncMethodNotUsingAwait()). This seems to work 100% but might just be a race condition!? I'm not so sure what is going on to be honest. This conclusion might be wrong and I risk my StackOverflow points here to hopefully learn from the comments :-P. Please do read them!

I just had the problem as described and found more info here.

The statement is: "you can't call an asynchronous method"

await asyncmethod2()

from a method that blocks

myAsyncMethod().Result

In my case I couldn't change the calling method and it wasn't async. But I didn't actually care about the result. As I remember it also didn't work removing the .Result and have the await missing.

So I did this:

public void Configure()
{
    var data = "my data";
    Task.Run(() => NotifyApi(data));
}

private async Task NotifyApi(bool data)
{
    var toSend = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
    await client.PostAsync("http://...", data);
}

In my case I didn't care about the result in the calling non-async method but I guess that is quite common in this use case. You can use the result in the calling async method.

Asynchronously deadlocked, or Do not wrap async methods into , GetAsync not returning) This is done using HttpClient class and WebAPI extensions of it. HttpResponseMessage response = await client. PrettyFoo asynchronously calls GetFooAsync and yields control of the thread. When I add a break point AFTER the code line await base.SendAsync above, I see that the code will never return, so I have no way to check if the response is get an 401 unauthorized to extract the Digest authorization header details. Nothing wrong at the API provider because I've successfully built another API consumer site using the traditional

httpClient.SendAsync never returns anything · Issue #10 · Domysee , My WPF application calls the bittrex api with a valid public and private key - with all httpClient.SendAsync never returns anything #10. Closed. marnixbent ConfigureAwait(false) (need to use this in another function where plain httpClient. Surprisingly, the same call from other function was working without ConfigureAwait(false) _httpRespMessage = await _httpClient.SendAsync(_httpRequest); (this worked in another function) _httpRespMessage = await _httpClient.SendAsync(_httpRequest).ConfigureAwait(false) (need to use this in another function where plain httpClient.SendAsync didn't

Understanding Async, Avoiding Deadlocks in C#, var request = await HttpClient. The same async await can be achieved by using ContinueWith and Unwrap. This type of code should be avoided, they should never be used in libraries This just defeated the whole purpose of async, the thread can no longer work on other tasks, it's blocked until the request finishes. var result = await httpClient.GetStreamAsync("weeklyplan") .ConfigureAwait(continueOnCapturedContext:false); Whether or not this is useful depends on how your code above is being called - in my case calling the async method using Task.GetAwaiter().GetResult() caused the code to hang.

HttpClient.GetAsync(…) never returns when using await/async , NET 4.5 – where “awaiting” the result of a call to (e.g.) httpClient.GetAsync() will never return. This only occurs in certain circumstances when  Consider applying the await operator to the result of the call. warning CS1998: This async method lacks await operators and will run synchronously. Consider using the await operator to await non-blocking API calls, or await Task.Run() to do CPU-bound work on a background thread.

Comments
  • How are you calling that async method? Doesn't it throw an exception?
  • Thanks, almost gave up on async/await before seeing this.
  • Me too! Why isn't this stuff better documented? Thanks again
  • Is it gonna happen if it's on neither UI context and ASP.NET context?
  • Awesome answer! But I am confused why so far I only have this issue when using HttpClient, it seems like the underlying implementation within HttpClient is not implemented correctly. The other workarounds I've found involve setting the current thread as STA which helps but is really indirect especially when you are using a 3rd party assembly and aren't aware that under the hood some call is going to wait definitely for a response that it is never going to get. In my case the dll was in-house, so we were able to ConfigureAwait... but it had to be done on the lowest level to the HttpClient obj.
  • @ChrisSchaller Make sure to read the full answer at stackoverflow.com/a/10351400/174735, which explains the issue fairly completely.