Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build .NET Core 2.0 EXE file with C# 7.1

Tags:

c#

.net

msbuild

I have a project I'm trying to build. It's using C# 7.1 features, and I can run it via Visual Studio, but when I try to publish to get an .exe file I get the error:

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]

The .csproj (project) file:

<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>

I'm building with:

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

Again, this all works when debugging in Visual Studio. Why is getting a simple .exe file out of a console application project template so awkward!

like image 911
Stuart Avatar asked Sep 07 '17 07:09

Stuart


People also ask

How do I create an EXE for .NET core console application?

First, right-click on the project and then hit Publish, then select folder and click create. Click the edit button to edit the configuration. In the publish configuration you can check the single EXE option. Once ready, save and click Publish.


1 Answers

Your problem is that in the section...

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

...you specify to use C# 7.1 in the Debug configuration.

However, with...

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

...you compile in the Release configuration.

You need to set up C# 7.1 in Release, too. You could also remove the condition entirely, which sets up the language version for any configuration.

like image 139
Sefe Avatar answered Oct 23 '22 12:10

Sefe