11

I am new to HttpClient. My code below always says "WaitingForActivation" in the status. Please help

private static async Task<HttpResponseMessage> MakeCall()
{
    var httpclient = new HttpClient();

    var response = await httpclient.GetAsync("http://localhost:60565/Subscribers");

    return response;
}
Esteban Verbel
  • 738
  • 2
  • 20
  • 39
user1615476
  • 161
  • 2
  • 2
  • 7

3 Answers3

15

Alternatively, if you environment is Synchronous add .Result, like so:

GetAsync("http://localhost:60565/Subscribers").Result;
JDandChips
  • 9,780
  • 3
  • 30
  • 46
3

That's normal. Just await the returned task to (asynchronously) wait for it to complete.

You may find my intro to async helpful.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
1

As Cleary wrote in his post, in order to create an asynchronous call your task should be awaited too. That means, the method in your question (MakeCall()) is asynchronous but probably the call to the method is synchronous.

An asynchronous example class:

using System.Threading.Tasks;

public class SampleClass
{
  public async Task<string> MakeCall()
  {
    # I am using the asynchronous call from the HttpClient library
    var response = await client.PostAsJsonAsync(url, creds)
    ...
  }
}

Try to await the call to the method.

var makeCall = await SampleClass.MakeCall();

What I would avoid is using .Result. As JDandChips already implies, it makes your call again synchronous. In this case, however, there is no need to try to make is asynchronous in the first place.

Alex_P
  • 2,580
  • 3
  • 22
  • 37