commentAsync .NET Core Console Apps feedback


Async .NET Core Console Apps

How to write an Async console application

It's a simple enough task. You want to write a console application using .NET Core and you want to use async/await to handle some long running tasks. Everything's going great until you try to mark the entry method in your console app as async:

static async Task Main(string[] args)
{ await yourCode(); }

And now it no longer compiles.

Visual Studio screen-shot of compilation error
Compilation error in Visual Studio

The compilation error "Program does not contain a static 'Main' method suitable for en entry point" is down to the fact that the C# language doesn't support an Async main method. That's a bit confusing.

C# 7.1 supports Async main methods but earlier versions do not. You guessed it, Visual Studio 2017 (at least at the time of writing) defaults to an earlier version. The fix is simply to select C# 7.1 or later as the version to compile to. It's a simple configuration change.

Visual Studio screen-shot of the application properties dialog
Location of the Advanced button on the project properties dialog

Open the project properties for your console app project and go to the build section.

Right down the bottom there's an "Advanced" button. This pops up a dialog that will allow you to change the target C# language version.

Visual Studio screen-shot of the C# version selector
Selecting the C# version to target

Notice that "C# latest major version (default)" is selected. Presumably, this means that C# 7.0 is being targeted.

Change the selection to either "C# latest minor version (latest)" or "C# 7.1".

Your application should now compile!

Visual Studio screen-shot of successful compilation
The project now compiles!

Got questions, comments?   Send me feedback