Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugger stops after async HttpClient.GetAsync() call in visual studio

I'm trying to test the follwing http request method

public async Task<HttpContent> Get(string url)     {         using (HttpClient client = new HttpClient()) // breakpoint         using (HttpResponseMessage response = await client.GetAsync(url)) // can't reach anything below this point         using (HttpContent content = response.Content)         {             return content;         }     } 

However, the debugger seems to be skipping the code below the 2nd comment. I'm using Visual studio 2015 RC, any ideas? I also tried checking the Tasks window and saw nothing

Edit: Found the solution

using System; using System.Net.Http; using System.Threading.Tasks;  namespace ConsoleTests {     class Program     {         static void Main(string[] args)         {             Program program = new Program();             var content = program.Get(@"http://www.google.com");             Console.WriteLine("Program finished");         }          public async Task<HttpContent> Get(string url)         {             using (HttpClient client = new HttpClient())             using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))             using (HttpContent content = response.Content)             {                 return content;             }         }     } } 

Turns out that because this was a C# console app it ended after the main thread ends I guess, because after adding a Console.ReadLine() and waiting a bit, the request did return. I guessed that C# would wait until my task execute and not end before it, but I suppose I was wrong. If anybody could elaborate on why this happened it would be nice.

like image 740
sgarcia.dev Avatar asked Jul 07 '15 23:07

sgarcia.dev


People also ask

What does HttpClient GetAsync return?

The HTTP request is sent out, and HttpClient. GetAsync returns an uncompleted Task .

What is HttpClient GetAsync?

GetAsync(Uri, HttpCompletionOption) Send a GET request to the specified Uri with an HTTP completion option as an asynchronous operation. GetAsync(Uri, CancellationToken) Send a GET request to the specified Uri with a cancellation token as an asynchronous operation.


Video Answer


1 Answers

When Main exits, the program exits. Any outstanding asynchronous operations are canceled and their results discarded.

So, you need to block Main from exiting, either by blocking on the asynchronous operation or some other method (e.g., calling Console.ReadKey to block until the user hits a key):

static void Main(string[] args) {   Program program = new Program();   var content = program.Get(@"http://www.google.com").Wait();   Console.WriteLine("Program finished"); } 

One common approach is to define a MainAsync that does exception handling as well:

static void Main(string[] args) {   MainAsync().Wait(); }  static async Task MainAsync() {   try   {     Program program = new Program();     var content = await program.Get(@"http://www.google.com");     Console.WriteLine("Program finished");   }   catch (Exception ex)   {     Console.WriteLine(ex);   } } 

Note that blocking on asynchronous code is generally considered a bad idea; there are very few cases where it should be done, and a console application's Main method just happens to be one of them.

like image 76
Stephen Cleary Avatar answered Sep 28 '22 01:09

Stephen Cleary