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

编程入门 行业动态 更新时间:2024-10-18 21:22:35
本文介绍了.NET Core是否支持异步控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在某个时间点,CoreCLR支持异步主入口点。参见 blog.stephencleary/2015/03/async -console-apps-on-net-coreclr.html

At some point in time the CoreCLR supported async main entry points. See blog.stephencleary/2015/03/async-console-apps-on-net-coreclr.html

但是,以下两个程序均不能在.NET Core RTM中运行

However both the following programs are not working in .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!"); } } }

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!"); } } }

这些都因错误而失败:

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

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

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

Are async console applications supported in .NET Core RTM?

推荐答案

是的,自 .NET Core 2.0 起,就支持 async Main 函数。

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

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

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>

或由

<LangVersion>7.1</LangVersion>

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

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>

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

where the Main function can be rewritten as following:

using System.Threading.Tasks; ... public static async Task Main(string[] args) { await BuildWebHost(args).RunAsync(); } ...

参考文献:

  • C#7系列,第2部分:异步Main
  • 冠军异步主程序(C#7.1)
  • C# 7 Series, Part 2: Async Main
  • Champion "Async Main" (C# 7.1)
  • 更多推荐

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

    本文发布于:2023-11-08 07:31:14,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1568734.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:控制台   应用程序   NET   Core

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!