C# 7.1 can't be published
Asked Answered
R

5

46

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?

Riva answered 22/12, 2017 at 19:55 Comment(8)
Project -> Project Properties (bottom) -> Build -> Advanced -> Language Version. Change to minor version (latest) or C# 7.1Kinsley
But I wonder how did you code without that setting? o_OKinsley
Thank you. As I said, I changed project version, so it compiles and runs fine. But even with this change it doesn't publish (I should have said language version for better clarity)Riva
@Felix, I just created a sample ASP.NET Core application, that uses the C#7.1 default literal, and published it to a folder, using both Visual Studio Enterprise 2017 and dotnet CLI - no errors. Could you maybe share your sample app with the problem?Jollanta
Thank you @ironstone13. I scaffolded ASP.NET Core Web application as well; then added a single line in Home controller int x = HttpContext.Items?.Count ?? default; and changed language to 7.1 (also tried 7.2, or latest - no difference). It compiles, but doesn't publish. I uploaded to github.com/virshu/SOExample But good news - it does publish with dotnet CLI. The only difference I see is that I have VS Community and you have Enterprise; so maybe the difference is indeed in the version of VSRiva
@Felix, I've cloned your repo, and I can build, publish and run the web app without any issues. It looks like an environment configuration issue to me. Your publish is using a different path to csc.exe then mine. I've uploaded my Visual Studio configuration details, as well as publish logs to Gist - maybe that can help you gist.github.com/oleh-zheleznyak/…Jollanta
thank you. I saw the difference in the path; strangely, the path to CLI app is the same as to Web App and it publishes fine. Whatever it is, publishing through dotnet cli is acceptable workaround, and in line with scripting deployment, anyway!Riva
FYI, I had this issue with .NET Framework, and the fix widely reported here did not help. The real problem was that my ASP.NET MVC application was targeting "Any CPU", while a referenced DLL was targeting x86. Once I adjusted the targeting to match, this extremely frustrating problem disappeared.Lucey
T
55

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

Twi answered 3/1, 2018 at 21:45 Comment(4)
This is exactly the miracle switch that I was looking for!Riva
In case someone is running this on the Mac version of Visual Studio, I was unable to find a .pubxml file or the configuration option mentioned in @Jollanta 's answer below. All I had to do was add <LangVersion>latest</LangVersion> under the <PropertyGroup> tags in my .csproj file and it got rid of this error! Visual Studio for Mac V 8.0.2Elvinelvina
Updated to 8.2 and the problem is fixed. Didn't need to add LangVersion.Disinherit
I don't get it. I just updated today to 15.9.20 in VS2017. Still an issue for me. However, the above fix does workMartlet
J
50

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>

enter image description here

Jollanta answered 22/12, 2017 at 20:3 Comment(10)
Thank you. As I said, I changed project version, so it compiles and runs fine. But even with this change it doesn't publish. (I should have said language version for better clarity)Riva
@Felix, did you set C#7.1 for Debug and Release configurations? You probably code in Debug but publish in ReleaseJollanta
just doublechecked; both release and debug are set up to C# 7.2 (or tried latest minor version as well)Riva
@Felix, I will try to reproduce later today and will get back to youJollanta
@Riva I see you marked this answer as solving your problem, but your comments seem to indicate that it isn't actually working for you. Did this actually fix your problem?Ogee
It didn't; but @Jollanta did amazing analysis and put the answer in the comments to original questionRiva
Good that you got your problem solved and you're certainly free to accept any answer you wish as the "one that answered your question" but keep in mind future visitors, that will now end up with the same conclusion you had, they did everything this answer told them to but they still cannot publish. As such the answer does not in fact answer your question.Bumgarner
@LasseVågsætherKarlsen, you are totally right - after the hint from Jeremy Cook I could finally repro the problem, so I updated my answer, and up-voted his answer. Now SO users will find the solution they look for!Jollanta
@Jollanta I've simplified and improved your configuration to work with latest minor version (e.g. 7.2, 7.3 etc), don't you mind?Manheim
The note to switch to "All Configurations" was key for me! I was apparently changing the wrong one and couldn't get it to build even after following many other guides.Sandfly
E
2

For MAC users, I did spend a long time finding out. Here's what has worked for me. Right-click to your main .csproj file and click 'Edit Project File' to open it. Then, inside the ... add the line latest and save it. That's it! Run your code and it should work ok from now on. add 'latest langversion' to visual studio

Escape answered 8/3, 2022 at 10:55 Comment(0)
M
0

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.

Manheim answered 14/6, 2018 at 13:26 Comment(0)
C
-3

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

Cart answered 22/12, 2017 at 20:38 Comment(1)
why do you make this conclusion? It's certainly not my intent; the target location shows as bin\Release\PublishOutput; and when I don't have 7.1 features in the code - it's exactly where the output is.Riva

© 2022 - 2024 — McMap. All rights reserved.