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?

8
  • 9
    Project -> Project Properties (bottom) -> Build -> Advanced -> Language Version. Change to minor version (latest) or C# 7.1
    – JohnyL
    Commented Dec 22, 2017 at 19:58
  • But I wonder how did you code without that setting? o_O
    – JohnyL
    Commented Dec 22, 2017 at 20:03
  • 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)
    – Felix
    Commented Dec 22, 2017 at 20:04
  • @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? Commented Dec 23, 2017 at 14:41
  • 1
    @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/… Commented Dec 23, 2017 at 20:19

5 Answers 5

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

4
  • This is exactly the miracle switch that I was looking for!
    – Felix
    Commented Jan 3, 2018 at 22:36
  • 7
    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 @ironstone13 '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.2
    – Jeff L
    Commented Apr 15, 2019 at 16:17
  • Updated to 8.2 and the problem is fixed. Didn't need to add LangVersion. Commented Jul 26, 2019 at 13:54
  • 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 work Commented Mar 4, 2020 at 22:56
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

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)
    – Felix
    Commented Dec 22, 2017 at 20:04
  • 7
    @Felix, did you set C#7.1 for Debug and Release configurations? You probably code in Debug but publish in Release Commented Dec 22, 2017 at 20:07
  • 2
    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. Commented Jan 3, 2018 at 21:56
  • 2
    @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! Commented Jan 4, 2018 at 20:15
  • 1
    @ironstone13 I've simplified and improved your configuration to work with latest minor version (e.g. 7.2, 7.3 etc), don't you mind? Commented Jun 14, 2018 at 13:21
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

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.

-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

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.
    – Felix
    Commented Dec 23, 2017 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.