Async call with await in HttpClient never returns
Asked Answered
D

4

115

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?

Demoss answered 27/3, 2012 at 18:3 Comment(1)
How are you calling that async method? Doesn't it throw an exception?Pargeting
G
172

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.

Grapeshot answered 29/4, 2012 at 2:39 Comment(9)
Thanks, almost gave up on async/await before seeing this.Lebel
Me too! Why isn't this stuff better documented? Thanks againPillion
Is it gonna happen if it's on neither UI context and ASP.NET context?Helenahelene
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.Adiaphorous
@ChrisSchaller Make sure to read the full answer at https://mcmap.net/q/42269/-httpclient-getasync-never-returns-when-using-await-async, which explains the issue fairly completely.Grapeshot
This is a good solution however I would recommend trying to find out what is stalling the await method in my case it was the host application that was using my custom code that uses asyncAgent
this worked perfectly for me on a disposable class that ran a bunch of http data repository calls at the same time. bet!Subphylum
Please read more into what ConfigureAwait(false) actually does before using it. It is not necessarily intended for this purpose, and there are plenty of side effects. For example, if you are using this in a website, you won't necessarily have access to things on the HttpContext, because you are no longer on the same context.Brentonbrentt
continueOnCapturedContext:false was the solution when using from ASP.NET websiteAmphibious
A
12

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.

Adriatic answered 4/7, 2019 at 4:22 Comment(2)
Not sure what you mean by this, it errors whenever I use an await operator in a non-asynchronous method (the controller's method). I cannot change it to be async either.Deemster
Example: public IHttpActionResult GetSomething() should be public async Task<IHttpActionResult> GetSomething() Then in your controller you forgot to await a function which makes a request using an http client, the response will never come back.Frit
H
0

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.

House answered 2/4, 2019 at 8:46 Comment(0)
D
-2

VS2022, C#, .Net Framework 4.7.2

I ran into a similar issue while trying to execute the below from a class and main class method both tagged async. It just never came back, and fiddler showed it did not get to the vendor's API server. Would get the waiting for activation message.

HttpCLient methodology. Did not ever return and matched the vendors recommended syntax for c#.

// Did not work
var response = await client.SendAsync(request);
var body = await response.Result.Content.ReadAsStringAsync();

Per a note our Lead Developer pointed me to in our teams development knowledge base - this resolved the issue. This worked.

// Works
var response = Task.Run(async () => await client.SendAsync(request));
var body = Task.Run(async () => await response.Result.Content.ReadAsStringAsync()); 

Hope it helps.

Dodgem answered 31/1 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.