使用C#7.1构建DotNet Core 2.0 Exe

时间:2017-09-07 07:23:44

标签: c# .net msbuild

我有一个我试图建立的项目。它使用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

同样,这在VS中进行调试时都能正常工作。为什么从ConsoleApplication项目模板中获取一个简单的.exe这么尴尬!

1 个答案:

答案 0 :(得分:39)

您的问题在于......

部分
<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中设置C#7.1。您也可以完全删除条件,为任何配置设置语言版本。