[英]Authenticating .net console applications with .net core web API
[英]Are async console applications supported in .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!");
}
}
}
或
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”方法
.NET Core RTM 是否支持異步控制台應用程序?
是的,從.NET Core 2.0
支持async Main
函數。
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,或者通過包含
<LangVersion>latest</LangVersion>
或由
<LangVersion>7.1</LangVersion>
例如對於 ASP.NET core 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>
其中 Main 函數可以重寫如下:
using System.Threading.Tasks;
...
public static async Task Main(string[] args)
{
await BuildWebHost(args).RunAsync();
}
...
參考資料:
更新:Async main 由 C# 7.1 原生支持! 請參閱上面 Evgeny 的回答。
我會為后代保留以下解決方法,但不再需要它。 async main
更簡單,如果可以,請使用它!
這是我在低於 7.1 的 C# 中首選的解決方法:
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()
是相同的.Wait
(同步阻塞),但是是優選的,因為它解開異常。
不久前刪除了對異步入口點的支持。
請在 aspnet/announcements github 上查看此問題。
我們決定將入口點語義與桌面 CLR 統一起來。
在 RC1 中已過時:
支持 async/Task<> Main。
支持入口點類型(程序)的實例化。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.