You should almost never use async void
methods and this is one reason why. Your Main()
will end before HttpClientCall()
actually completes. And since exiting from Main()
terminates the whole application, nothing will get printed.
What you should do is to change your method into async Task
and Wait()
for it in your Main()
. (Mixing await
and Wait()
can often lead to deadlocks, but it's the right solution for console applications.)
class MainClass
{
public static void Main()
{
new Client().HttpClientCallAsync().Wait();
}
}
public class Client
{
HttpClient client = new HttpClient();
public async Task HttpClientCallAsync()
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://vendhq.com");
string responseAsString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseAsString);
}
}