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.