I was working hard today on a .NET Core 2.0 console app that kept disappearing when I hit a line of code like this:
bool exists = await blob.ExistsAsync();
This, however, would work:
bool exists = blob.ExistsAsync().Result;
I knew that something must be wrong with the environment for the async / await asynchronous task keywords to misbehave, but it took me some googling to figure out what it was.
Changing a .NET Core 2.0 Console app to work with async and await
1. Make sure that your Main function has the async keyword:
static async Task Main(...)
You may also need to add a using statement for Task:
using System.Threading.Tasks;
2. You’ll probably need to set your compiler to C# 7.1. This is part of the project’s settings. At the time of writing, C# 7.0 is the default, and doesn’t support async Main(...)
3. Make sure to properly use async and await throughout your code. This was important for me, since I had been trying different things to get my app to work. You really don’t want to mix .Wait and .Result with async and await, as you can get SyncronizationContext issues.