user1615476
user1615476

Reputation: 161

HttpClient GetAsync always says 'WaitingForActivation'

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;
}

Upvotes: 11

Views: 21495

Answers (3)

Alex_P
Alex_P

Reputation: 2952

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.

Upvotes: 1

JDandChips
JDandChips

Reputation: 10100

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

GetAsync("http://localhost:60565/Subscribers").Result;

Upvotes: 15

Stephen Cleary
Stephen Cleary

Reputation: 456377

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

You may find my intro to async helpful.

Upvotes: 3

Related Questions