C# 7.1 can't be published

旧巷老猫 提交于 2019-12-18 14:12:24

问题


I have ASP.NET Core C# web application. I made some changes that now use C# 7.1 features. I changed project version, so it compiles and runs fine. However, when I try to publish the project, I am getting an error:

Feature 'default literal' is not available in C# 7.0. Please use language version 7.1 or greater.

Compile command that I see is:

C:...\.nuget\packages\microsoft.net.compilers\2.6.1\tools\csc.exe /noconfig /unsafe- /checked- /nowarn:1701,1702,1705,1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE;RELEASE;NETCOREAPP2_0 /errorendlocation /preferreduilang:en-US /warnaserror+:NU1605`

As suggested elsewhere, I installed Microsoft.Net.Compilers (v2.6.1), but it didn't make any difference.

Is there a Visual Studio setting that affects publish specifically?

UPDATE: Looks like a console application doesn't have this problem. If it builds successfully, it publishes successfully as well. However, the web application does not publish. Was anybody successful in publishing ASP.NET Core web application with C# 7.1 features?


回答1:


Adding <LangVersion>latest</LangVersion> to your .pubxml file made it possible for Visual Studio 2017 (15.5.2 in my case) to publish.

Source: https://developercommunity.visualstudio.com/solutions/166543/view.html




回答2:


Update:
After upgrading my VS2017 from version 15.4.5 to 15.5.2 I can reproduce the problem, and I get an error

Feature 'default literal' is not available in C# 7.0. Please use language version 7.1 or greater

The answer from @Jeremy Cook solves the issue:
<LangVersion>latest</LangVersion> in .pubxml


In both old and new project formats the LangVersion element in project file is responsible for this. You can either change that via csproj xml file or via UI in visual studio.

Please note that this setting is dependent on your build configuration. To make sure that you can both code and publish using C# 7.1 and later make sure you configure this setting regardless of build configuration (Debug, Release etc).

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

</Project>




回答3:


If you are migrating from ASP.NET Core 2.0 to ASP.NET Core 2.1 make sure you have line

<TargetFramework>netcoreapp2.1</TargetFramework>

in your .pubxml file.




回答4:


It seems you are published to your local Nuget store. Ensure that the Nuget store is configured to use C#7.1. And also check whether your Nuget.exe pack is updated to the latest that can use C#7.1



来源:https://stackoverflow.com/questions/47946732/c-sharp-7-1-cant-be-published

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!