15

I have several projects in the solution, and the C# 7 features, such as tuples and throw expressions, work fine in all of the library projects, but there is a (non Core) web project that doesn't compile due to errors on the C# 7 features. Right after compiling, the error window quickly clears itself, presumably because the IDE/editor compiles the same units without error. I have to use the output window to see the compiler errors. It is as though the IDE/editor are assuming C# 7, but the compiler used in the build is not.

I've tried adding "__DEMO__,__DEMO_EXPERIMENTAL__" to the conditional compilation symbols, to no avail. I've experimented with targeting different version of the framework and have edited the web.config, including the compilation and targetFramework tags of system.web.

Example errors:

if (!config.Properties.TryGetValue(modelId, out var model)) // error CS1003: Syntax error, ',' expected
if (modelDescription is ComplexTypeModelDescription complexTypeModelDescription) // error CS1026: ) expected

Here are the first few lines of the csproj file for the project:

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

Update: I tried creating a new web project using the latest template in VS 2017 RC and copying in my source files. Same thing.

I also tried explicitly setting Project | Properties | Build | Advanced | Language Version to 7. Results in "Invalid option '7' for /langversion".

N8allan
  • 2,138
  • 19
  • 32

2 Answers2

24

The solution is to update the Microsoft.Net.Compilers nuget package to >=2.0.0. As of now, for the 2.0.0 version to appear, the "Include prerelease" checkbox at the top of the package manager must be checked. With this installed, the Language version advanced setting doesn't have to be overridden from Default.

N8allan
  • 2,138
  • 19
  • 32
  • 1
    Been spending over an hour building and cleaning my project with it failing everytime with a really fague error. I even reinstalled VS2017. Then I found your answer and it immediately solved the problem. Thank you! – jao Mar 23 '17 at 20:33
  • 1
    I wonder y this answer is not marked as the answer! Thanks a lot! – Heba Gomaah Apr 30 '17 at 18:37
  • Why does this work? Is the dll less than version 2.0 imposing some kind of "max language level" on the project? I noticed with v 1.3 installed, I cannot set the project type to use language version 7, even though it's a selectable option. I get an error saying it must be version 1 - 6. Somehow that DLL seems to be limiting the maximum allowed language version of the project. – Triynko Jun 08 '17 at 22:09
  • @Triynko, Yeah, the reason is that the C# 7 compiler features are _implemented in the new version of that dll_. Older compiler -> older language version. – Rytmis Aug 11 '17 at 10:17
1

I got here trying to answer why C #7 features weren't working in VS2017. I had just upgraded an existing project just like the answer said, and at the end had gotten a message about the compiler DLL being in use and VS needing a restart.

Afterward I could type C# 7 code in the IDE fine, but it would always fail on compilation of any new feature usage. Turns out the .csproj file itself had imports for the new and old compiler like below. These did not show in the references list in the project. I deleted the old imports lines and was good to go.

<?xml version="1.0" encoding="utf-8"?><Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="packages\Microsoft.Net.Compilers.2.8.2\build\Microsoft.Net.Compilers.props" Condition="Exists('packages\Microsoft.Net.Compilers.2.8.2\build\Microsoft.Net.Compilers.props')" />
  <Import Project="..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
  <Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.1\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.1\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
…
KnarfaLingus
  • 456
  • 2
  • 10