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?

poke
  • 369,085
  • 72
  • 557
  • 602
Felix
  • 9,248
  • 10
  • 57
  • 89
  • 9
    `Project -> Project Properties (bottom) -> Build -> Advanced -> Language Version`. Change to `minor version (latest)` or `C# 7.1` – JohnyL Dec 22 '17 at 19:58
  • But I wonder how did you code without that setting? o_O – JohnyL Dec 22 '17 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 Dec 22 '17 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? – ironstone13 Dec 23 '17 at 14:41
  • 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 https://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 VS – Felix Dec 23 '17 at 18:43
  • 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 https://gist.github.com/oleh-zheleznyak/458230bb535366c378fc3b7380fbc2bf – ironstone13 Dec 23 '17 at 20:19
  • 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! – Felix Dec 23 '17 at 21:24
  • 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. – Daniel Feb 11 '20 at 21:07

5 Answers5

53

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

Jeremy Cook
  • 20,840
  • 9
  • 71
  • 77
  • This is exactly the miracle switch that I was looking for! – Felix Jan 03 '18 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 `latest` under the `` tags in my .csproj file and it got rid of this error! Visual Studio for Mac V 8.0.2 – Jeff L Apr 15 '19 at 16:17
  • Updated to 8.2 and the problem is fixed. Didn't need to add LangVersion. – Chucky Jul 26 '19 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 – AlbatrossCafe Mar 04 '20 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

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
ironstone13
  • 3,325
  • 18
  • 24
  • 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 Dec 22 '17 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` – ironstone13 Dec 22 '17 at 20:07
  • just doublechecked; both release and debug are set up to C# 7.2 (or tried latest minor version as well) – Felix Dec 23 '17 at 00:26
  • @Felix, I will try to reproduce later today and will get back to you – ironstone13 Dec 23 '17 at 09:44
  • @Felix 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? – Bradley Uffner Dec 28 '17 at 13:52
  • It didn't; but @ironstone13 did amazing analysis and put the answer in the comments to original question – Felix Dec 30 '17 at 17:43
  • 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. – Lasse V. Karlsen Jan 03 '18 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! – ironstone13 Jan 04 '18 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? – Vadim Ovchinnikov Jun 14 '18 at 13:21
  • 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. – cscott530 Feb 19 '19 at 12:20
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.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
-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

Shankar
  • 18
  • 4
  • 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 Dec 23 '17 at 00:28