.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 : 프로그램에 진입 점에 적합한 정적 '기본'메서드가 없습니다.
.NET Core RTM에서 비동기 콘솔 애플리케이션이 지원 되나요?
예, 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
async Main함수에 대한 지원 은 C # 버전 7.1에 도입되었습니다. 그러나이 기능은 기본적으로 사용할 수 없습니다. 이 기능을 사용하려면 다음 .csproj을 포함 하여 파일 에 C # 버전 7.1을 명시 적으로 지정해야합니다.
<LangVersion>latest</LangVersion>
또는
<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.
As Nick said, support for this was removed. This is my preferred workaround:
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.
There is a proposal for adding async Main() to a future version of C#: csharplang#97
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
참고URL : https://stackoverflow.com/questions/38114553/are-async-console-applications-supported-in-net-core
'IT박스' 카테고리의 다른 글
| 템플릿의 Angular 2 해시 태그는 무엇을 의미합니까? (0) | 2020.08.13 |
|---|---|
| 웹 사이트에 인도 통화 기호 표시 (0) | 2020.08.13 |
| Linux bash : 다중 변수 할당 (0) | 2020.08.13 |
| PDO 폐쇄 연결 (0) | 2020.08.12 |
| Mockito의 argumentCaptor에 대한 예 (0) | 2020.08.12 |