.NET Core是否支持异步控制台应用程序?

kim*_*gro 107 .net c# .net-core

在某些时候,CoreCLR支持异步主入口点.见http://blog.stephencleary.com/2015/03/async-console-apps-on-net-coreclr.html

但是,以下程序在.NET Core RTM中都不起作用

using System;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            await Task.Delay(1000);
            Console.WriteLine("Hello World!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

using System;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Program
    {
        public async Task Main(string[] args)
        {
            await Task.Delay(1000);
            Console.WriteLine("Hello World!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这两个都失败了,错误:

错误CS5001:程序不包含适用于入口点的静态"主"方法

.NET Core RTM是否支持异步控制台应用程序?

Evg*_*kin 163

是的,async Main从那时起支持这些功能.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
Run Code Online (Sandbox Code Playgroud)

async MainC#版本7.1中引入了对函数的支持.但是,此功能无法开箱即用.要使用此功能,您需要在.csproj文件中明确指定C#版本7.1 ,方法是包含

<LangVersion>latest</LangVersion>
Run Code Online (Sandbox Code Playgroud)

或者

<LangVersion>7.1</LangVersion>
Run Code Online (Sandbox Code Playgroud)

例如,对于ASP.NET核心2.0项目:

<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>
Run Code Online (Sandbox Code Playgroud)

其中Main函数可以重写如下:

using System.Threading.Tasks;

...
public static async Task Main(string[] args)
{
   await BuildWebHost(args).RunAsync();
}
...
Run Code Online (Sandbox Code Playgroud)

参考文献:

  1. C#7系列,第2部分:异步主要
  2. 冠军"Async Main"(C#7.1)

  • 您还可以在项目属性中设置语言版本(现在?); 构建 - >高级 - >语言版本. (6认同)

Nat*_*ini 49

更新:C#7.1原生支持Async main!见上面的Evgeny的答案.

我将为后代保留以下解决方法,但不再需要它.async main更简单.


正如尼克所说,对此的支持被删除了.这是我首选的解决方法:

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!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

GetAwaiter().GetResult().Wait(同步阻塞)相同,但是首选,因为它会解除异常.

有一个建议添加async Main()到C#的未来版本:csharplang#97


Nic*_*sta 10

一段时间后删除了对异步入口点的支持.

在aspnet/announcements github上查看此问题.

我们决定采用桌面CLR统一入口点语义.

在RC1中已过时:

支持async/Task <> Main.

支持实例化入口点类型(Program).

Main方法应该是public static void Main或public static int Main.

支持将依赖项注入到Program类的构造函数和Main方法中.

请改用PlatformServices和CompilationServices.

要获取IApplicationEnvironment,IRuntimeEnvironment,IAssemblyLoaderContainer,IAssemblyLoadContextAccessor,ILibraryManager使用Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default静态对象.

要访问ILibraryExporter,ICompilerOptionsProvider使用Microsoft.Extensions.CompilationAbstractions.CompilationServices.Default静态对象.

支持CallContextServiceLocator.请改用PlatformServices和CompilationServices.

与上述相同.

这些将在RC2中删除:#106