【问题标题】:Build .NET Core 2.0 EXE file with C# 7.1使用 C# 7.1 构建 .NET Core 2.0 EXE 文件
【发布时间】:2019-09-12 21:17:09
【问题描述】:

我有一个正在尝试构建的项目。它使用 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 文件如此尴尬!

【问题讨论】:

    标签: c# .net msbuild


    【解决方案1】:

    你的问题是在那个部分...

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

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

    但是,随着...

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

    ...你在 Release 配置中编译。

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

    【讨论】:

    • 谢谢!
    • @Stuart:我的荣幸。
    • @Stuart 有什么特别的理由为调试和发布分别设置这个吗?它也可以在所有配置的第一个 PropertyGroup 部分中设置。
    • @ZevSpitz 你说得对,绝对没有理由指定条件。
    • @ZevSpitz:我已经编辑了答案。它现在包括删除条件的选项。
    猜你喜欢
    • 2016-09-20
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2018-07-09
    • 2018-02-14
    相关资源
    最近更新 更多