12

Current project:

  • ASP.NET 4.6.2
  • MVC 5
  • Visual Studio 2015 Community v14.0.25431.01 Update 3
  • Installed the CodeDOM Providers for .NET Compiler nuget package via Project -> Install C# 6
  • Confirmed that I do have the compiler element in my Web.Config and that it is referencing C# 6

My Razor code:

@if(Session["Type"]?.ToString() == "Insurance") {
  <text>policy of insurance</text>
} else if(Session["Type"]?.ToString() == "Warranty") {
  <text>policy of warranty</text>
} else {
  <text>protection policy</text>
}

Visual Studio explicitly flags this with an error,

Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater.

And Visual Studio is explicitly using C# 6:

enter image description here

When I try to run the page itself, I get an error:

CS1525: Invalid expression term '.'

which shows that it is explicitly going after the period following the question mark.

This project was explicitly started as a C# 6 project, and I am quite confused as to why it is derping back to C#5.

Another strange issue: this is working just fine both in debug as well as on my local test setup (I publish to the filesystem, view using local IIS), but when I upload to the server (which has many, many other C#6 sites) it craps out. This is the first razor-side quirk I have ever run across that actually throws an error this badly.

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
René Kåbis
  • 842
  • 2
  • 9
  • 28
  • 2
    NB: The correct operator for null-coalescing is `??`, and `?.` is null-conditional operator. Since CodeDOM provider for Roslyn used there, have you either activated Copy Local option for related assemblies or setting target framework properly? – Tetsuya Yamamoto Jun 07 '17 at 02:05
  • 1
    Sorry, it was late in the day, I was tired and had a headache. You are correct, I am trying to make use of the null-conditional operator. Copy Local has been set to True for `Microsoft.CodeDom.Providers.DotNetCompilerPlatform`. The target framework was set to DotNet 4.6.2 when I first created the solution. All projects in the solution are using the same framework. – René Kåbis Jun 07 '17 at 18:05
  • Version of framework isn't related with compiler version. – VMAtm Jun 09 '17 at 20:55
  • @VMAtm you will have to be a bit more clear. I have specified C# 6 in the Advanced Build window of my project properties, and I have explicitly upgraded the project itself to C#6 via the `Project -> Install C#6` menu option. C#6 usage by the compiler has been confirmed via the compiler element in the `web.config`. What else should I be doing? – René Kåbis Jun 09 '17 at 21:57
  • I meant that you have to update either `DotNetCompilersPlatform` and `Compilers` packages and set your project to `c# 7.0`. Related: https://stackoverflow.com/a/44384196/213550 – VMAtm Jun 12 '17 at 15:23
  • Could it be that in more recent VS/.net-versions explicit targeting has become more or less redundant? (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version) – qqtf Aug 10 '22 at 08:46

1 Answers1

11

I was having a similar problem and found that I was missing the following from my Web.config file:

<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
</system.codedom>
Dharman
  • 30,962
  • 25
  • 85
  • 135