In . NET Core C# we make use of async and await keywords to implement asynchronous programming. For any method to be asynchronous we have to add the async keyword in the method definition before the return type of the method.
An easy fix is to move the async code block into a separate method that you can mark with async and call from Main : class Program { static void Main(string[] args) { // Call SomeAsyncCode() somehow } private static async Task SomeAsyncCode() { // Use await here! await Task. Delay(10); // Other async goodness... } }
NET Core Console application to run with dependency inversion, configuration, and Entity Framework — all things you would likely want in a console application (Note, we won't use Entity Framework in this example).
We add an async keyword to the method signature, modify the return type by using Task , and we use the await keyword when we call the GetAllCompanies awaitable method. The rest of the code – the mapping part, the logging part, and the return of the result – will be executed after the awaitable operation completes.
Yes, the async Main
functions are supported since .NET Core 2.0
.
dotnet --info
.NET Command Line Tools (2.0.0)
Product Information:
Version: 2.0.0
Commit SHA-1 hash: cdcd1928c9
Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
Base Path: /usr/share/dotnet/sdk/2.0.0/
Microsoft .NET Core Shared Framework Host
Version : 2.0.0
Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d
The support for the async Main
functions is introduced in C# version 7.1. However, this functionality is not available out of the box. To make use of this feature you need explicitly specify the C# version 7.1 in your .csproj
file, either by including
<LangVersion>latest</LangVersion>
or by
<LangVersion>7.1</LangVersion>
For example for the ASP.NET core 2.0 project:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
where the Main function can be rewritten as following:
using System.Threading.Tasks;
...
public static async Task Main(string[] args)
{
await BuildWebHost(args).RunAsync();
}
...
References:
Update: Async main is supported natively by C# 7.1! See Evgeny's answer above.
I'll keep the below workaround for posterity, but it is no longer needed. async main
is way simpler, use that if you can!
This is my preferred workaround in C# less than 7.1:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
Console.ReadKey();
}
public static async Task MainAsync(string[] args)
{
await Task.Delay(1000);
Console.WriteLine("Hello World!");
}
}
}
GetAwaiter().GetResult()
is the same as .Wait
(blocking synchronously), but is preferred because it unwraps exceptions.
Support for async entry points was removed a while back.
See this issue on the aspnet/announcements github.
We decided to move towards unification of entry point semantics with desktop CLR.
Obsolete in RC1:
Support for async/Task<> Main.
Support for instantiating of entry point type (Program).
The Main method should be public static void Main or public static int Main.
Support for injecting dependencies into the Program class's constructor and Main method.
Use PlatformServices and CompilationServices instead.
To get to IApplicationEnvironment, IRuntimeEnvironment, IAssemblyLoaderContainer, IAssemblyLoadContextAccessor, ILibraryManager use Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default static object.
To get to ILibraryExporter, ICompilerOptionsProvider use the Microsoft.Extensions.CompilationAbstractions.CompilationServices.Default static object.
Support for CallContextServiceLocator. Use PlatformServices and CompilationServices instead.
Same as above.
These would be removed in RC2: #106
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With