Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 968
user1615476 Avatar asked Sep 06 '13 08:09

user1615476


People also ask

What does HttpClient GetAsync return?

The HTTP request is sent out, and HttpClient. GetAsync returns an uncompleted Task . AsyncAwait_GetSomeDataAsync awaits the Task ; since it is not complete, AsyncAwait_GetSomeDataAsync returns an uncompleted Task . Test5Controller. Get blocks the current thread until that Task completes.

What is GetAsync C#?

The GetAsync method sends a GET request to the specified Uri as an asynchronous operation. The await operator suspends the evaluation of the enclosing async method until the asynchronous operation completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.

What does GetAsync return?

:GetAsync() returns nil whenever Data is created “True nil” output comes from loadData() function, “true true” output comes from checkData() function.

What is HttpClient SendAsync?

SendAsync(HttpRequestMessage, HttpCompletionOption) Send an HTTP request as an asynchronous operation. SendAsync(HttpRequestMessage, CancellationToken) Send an HTTP request as an asynchronous operation.


2 Answers

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

GetAsync("http://localhost:60565/Subscribers").Result;
like image 97
JDandChips Avatar answered Sep 16 '22 15:09

JDandChips


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.

like image 45
Alex_P Avatar answered Sep 18 '22 15:09

Alex_P