gpt4 book ai didi

c# - 使用 C# 7.1 构建 .NET Core 2.0 EXE 文件

转载 作者:IT王子 更新时间:2023-10-29 04:29:45 26 4
gpt4 key购买 nike

我有一个正在尝试构建的项目。它使用 C# 7.1 功能,我可以通过 Visual Studio 运行它,但是当我尝试发布以获取 .exe 文件时出现错误:

Agent.cs(8,30): error CS8107: Feature 'async main' is not available in C# 7.
Please use language version 7.1 or greater. [C:\Users\stuarts\Documents\Visual
Studio 2017\Projects\Agent\Agent\Agent.csproj]
CSC : error CS5001: Program does not contain a static 'Main' method suitable
for an entry point [C:\Users\stuarts\Documents\Visual Studio
2017\Projects\Agent\Agent\Agent.csproj]

.csproj(项目)文件:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
<RuntimeFrameworkVersion>2.0.0-*</RuntimeFrameworkVersion>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RabbitMQ.Client" Version="5.0.1" />
</ItemGroup>

</Project>

我正在构建:

dotnet publish -c Release -r win10-x64 Agent.csproj

同样,在 Visual Studio 中调试时这一切都有效。为什么从控制台应用程序项目模板中获取一个简单的 .exe 文件如此笨拙!

最佳答案

您的问题是在该部分...

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

...您指定在调试配置中使用 C# 7.1。

但是,随着...

dotnet publish -c Release -r win10-x64 Agent.csproj

...您在发布配置中编译。

您还需要在 Release 中设置 C# 7.1。您也可以完全删除条件,这会为任何配置设置语言版本。

关于c# - 使用 C# 7.1 构建 .NET Core 2.0 EXE 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46090274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com