Aspnetcore: Publish with async Main - Error CS8107: Feature 'async main' is not available in C# 7.0

Created on 9 Jan 2018  路  17Comments  路  Source: dotnet/aspnetcore

When I publish an ASP.NET Core Web API that has an async Main method: public static async Task Main, I get the following error:

Program.cs(13,29): Error CS8107: Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater.
CSC(0,0): Error CS5001: Program does not contain a static 'Main' method suitable for an entry point

This occurs even _after_ setting the language version of each project in the solution to C# 7.2 for _both Debug and Release_ build configurations.

Visual Studio 2017 Version: 15.5.2
OS Version: Windows 10 Pro Version 1709

Most helpful comment

@tonysneed I wonder if a different Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" is used in that case?

At least for debugging purposes, can you try moving the <LangVersion>latest</LangVersion> to the main <PropertyGroup> at the top? I always do that manually in my own CSPROJ files because I feel obligated to "refactor" my CSPROJ files 馃槃

All 17 comments

@tonysneed can you show what your CSPROJ looks like? And also, after you publish your app, it's already been compiled, so where is the new compilation error coming from? A deployed app should just be DLL files, no CS files anymore.

I had that happen to me recently on my Linux build machine. The reason was that I had only SDK 2.0 installed and the fix was adding 2.1 as well.

Here is the csproj file for my ASPNETCORE Web Api:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
  </PropertyGroup>

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

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

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\PlexClaims.Abstractions\PlexClaims.Abstractions.csproj" />
    <ProjectReference Include="..\PlexClaims.ActiveDirectory\PlexClaims.ActiveDirectory.csproj" />
    <ProjectReference Include="..\PlexClaims.Data\PlexClaims.Data.csproj" />
    <ProjectReference Include="..\PlexClaims.Entities\PlexClaims.Entities.csproj" />
  </ItemGroup>

</Project>

Notice that LangVersion is latest (vs default), which is the value for all projects in the solution, both for Debug and Release.

Here is the Publish Profile I'm using:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>73d94c7c-d1a9-43f1-a12d-1e5abd08915d</ProjectGuid>
    <publishUrl>bin\Release\PublishOutput</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

@tonysneed thanks for sharing those. Do you have more info about what is happening when the error shows up? Once the app is published, there's no Program.cs file to compile, so I'm at a loss as to how that is happening at all. At most there would be some CSHTML files being compiled (they get converted to CS files, then compiled with CSC.exe), but not the Program.cs file.

@Eilon Yes, the error takes place when the Publish command is _building_ the project. Because the build step of Publish fails, the web app is never published.

Oh I see, it's the publish process itself, not the published app.

@vijayrkn - any idea why the publish process would use different C# settings than a regular build?

@tonysneed I wonder if a different Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" is used in that case?

At least for debugging purposes, can you try moving the <LangVersion>latest</LangVersion> to the main <PropertyGroup> at the top? I always do that manually in my own CSPROJ files because I feel obligated to "refactor" my CSPROJ files 馃槃

@Eilon - Don't know why this is happening. I will take a look.

@tonysneed Did publish succeed after moving the LangVersion property to the main PropertyGroup?

I'll try that. Need a bit of time.

@vijayrkn Moving LangVersion to the main property group fixed the issue for me. It looks like Publish ignores the Condition?

Looks like an issue with the handling of the platform name in publish. As a work-around you can either move the property outside or add these additional conditions.

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU' or '$(Configuration)|$(Platform)'=='Debug|Any CPU'">
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU' or '$(Configuration)|$(Platform)'=='Release|Any CPU'">
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

@vijayrkn should this bug go to https://github.com/aspnet/websdk ?

This is a bug in the VS side. VS bugs are tracked in developer community.

Found another bug in the developer community for the same issue.
https://developercommunity.visualstudio.com/content/problem/160971/migrating-from-vs2017-1545-to-1550-causes-cs8107-e.html

This bug can be closed as dupe and we can use the developer community bug to track the issue progress.

Thanks @vijayrkn !

I got the same error during build time. I used the .NET Core CLI.

Adding the LangVersion to the main PropertyGroup fixed the issue for me.

I got the same error building via Azure DevOps; When I checked the project file, it didn't have the LangVersion property at all, although VS Designer show that the default(latest) is selected.

Issue got resolved after manually setting the value to Latest.

Was this page helpful?
0 / 5 - 0 ratings