使用C# 7.1构建.NET Core 2.0可执行文件

23

我有一个项目正在尝试构建。它使用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文件这么麻烦!

1个回答

43

你的问题在于该部分的...

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

...您可以指定在 Debug 配置中使用 C# 7.1。

但是,与...

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

......在 Release 配置下编译。

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


谢谢你! - Stuart
@Stuart:不客气。 - Sefe
1
@Stuart 有没有特别的原因要分别为Debug和Release设置这个?它也可以在第一个“PropertyGroup”部分中为所有配置设置。 - Zev Spitz
@ZevSpitz 你说得对,毫无必要指定条件。 - Monsignor
@ZevSpitz:我已经编辑了答案。现在它包括删除条件的选项。 - Sefe
很好,我其实很惊讶7.1特性在VS中不是默认的全局属性。感谢您的回答。 - The Senator

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接