22

I'm using Visual Studio 17 (version 15.8.5), my project targets .NET Framework 4.8 and I've tried setting the C# version to use (via Build tab in the Properties window) C# 7.3 (that's the maximum version selectable from the dropdown list). However building the project reports a failure like what in the question title. I've even tried 7.0 and C# latest minor version, all those report failure as well.

Looks like the environment supports the maximum version of 6 (C# 6.0)?

I've tried installed both Microsoft.Net.Compilers v3.6.0 and Microsoft.Net.Compilers.Toolset v3.6.0 but it changed nothing.

I would like to use the pattern matching feature as the following simple code does:

object o ...
if(o is int c){
    //do something with c (cast to int) ...
}

UPDATE:

It's strange that if I create a completely new ASP.NET MVC project , it just compiles OK with C# 7.3 set. So looks like there is something wrong with just the specific project I'm working on. For better diagnosing, I would like to share that project here via this link https://sites.google.com/site/ngaodaantoi/files/WebApiAuth.zip?attredirects=0&d=1

It's recommended that you have VS 2017 to help possibly reproduce the issue.

Hopeless
  • 4,397
  • 5
  • 37
  • 64
  • 2
    To troubleshoot this, I would confirm what version of the compiler is being used by putting `#error version` in source (this will output the version of the compiler) and also use `/langversion:?` to list available versions. – Julien Couvreur Jul 12 '20 at 15:29
  • @JulienCouvreur I've tried what you suggested and it shows `Compiler version: '2.9.0.63208 (958f2354)'` the `langversion` shows exactly what I set via the project's Properties window. Thanks for your suggestion anyway however I have no clue about that info. – Hopeless Jul 12 '20 at 16:04
  • Do you have the most recent version of the Microsoft.Net.Compilers Nuget package installed? – DMadden51 Jul 12 '20 at 23:07
  • @DMadden51 yes I do have that (already mentioned in my question), installed the latest version of `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` as well. – Hopeless Jul 13 '20 at 03:38
  • @Hopeless I am mystified :-/ Roslyn compiler version 2.9.x support language version 7.3 (documented [here](https://github.com/dotnet/roslyn/wiki/NuGet-packages#versioning)). It may be worth asking the ASP.NET team. There can be more than one compiler involved: IDE, Build and ASP.NET. Looking at a build log would help (use `/binarylog` option then "MSBuild structured log viewer"). If your project is small/simple/standalone and not confidential, you could share it with me to try and repro (email jcouv@microsoft.com). – Julien Couvreur Jul 13 '20 at 21:16
  • @JulienCouvreur I've tried installing the extension `Project System Tools` for VS 2017 and can see the binlog result but looks like it's no help, the error path is `Target CoreCompile -> Target Csc`. It's ridiculous that a completely new ASP.NET MVC/WebApi project can be built fine with C# 7.3 set. I'm not sure what could be wrong with the project I have the issue with (I've tried comparing the 2 projects to see the difference but it's hard except the list of installed packages). My project is just a demo project (something like a playground) and has nothing confidential here.... – Hopeless Jul 14 '20 at 05:10
  • @JulienCouvreur please download that project via the following link: https://sites.google.com/site/ngaodaantoi/files/WebApiAuth.zip?attredirects=0&d=1 I've modified the `Web.config` file with `/langversion` set back to `default` to see if it builds OK but it had no effect. Please share with me any info you could find about what is wrong with that project. Thank you! – Hopeless Jul 14 '20 at 05:11
  • Also when creating a completely new ASP.NET MVC project, I can see that the package `Microsoft.Net.Compilers` is not installed but still the project is built fine with C# 7.3 set. So I tried uninstalling that package (which was mentioned as installed in my original question) from the project I've shared. – Hopeless Jul 14 '20 at 05:17
  • @Hopeless I downloaded your project to try and repro, but then realized I only have VS 17+ installed. Sorry I couldn't get to the bottom of this. I'd recommend upgrading or pinging the ASP.NET folks (file an issue via VS feedback for example). – Julien Couvreur Jul 15 '20 at 15:08
  • no problem, as I said it's just a demo project and VS 2017 supports C# 7.x well (because a new project compiles well), not sure what specifically wrong with the project I've shared with you but I can just keep it there for later trying to figure it out when I have free time. Thank you for your support anyway. – Hopeless Jul 15 '20 at 15:24

7 Answers7

41

I resolved this issue wihout upgrading any NuGet packages by following these steps:

  1. Close all open instances of Visual Studio

  2. Open "Visual Studio Installer"

  3. Click "Modify" on the version of Visual Studio you're using

    i. In my case, I'm using VS2019 Professional 16.11.0

  4. At the top of the dialog, select the "Individual Components"

  5. In the search bar, type "Roslyn"

  6. Check both options, should look like this

    i. In case the image goes down, the options are ".NET Compiler Platform SDK" and "C# and Visual Basic Roslyn Compilers"

    ii. In my case, the second one was already checked

  7. Confirm changes by pressing "Modify" at the bottom right of the dialog

Once I reopened my project, I was able to compile successfully.

Justin C
  • 786
  • 8
  • 17
21

Try upgrading below NuGet packages (whichever is installed) to resolve the problem:

-Microsoft.CodeDom.Providers.DotNetCompilerPlatform

-Microsoft.Net.Compilers

ketan27j
  • 393
  • 3
  • 9
  • This worked for me locally but it failed on the build server when I committed the changes with something to do with GetPathsOfAllDirectoriesAbove(). My build server wasn't originally reporting any problems, so before going down that rabbit hole, I found this solution: https://stackoverflow.com/a/64542445/1158234. I simply updated VS 2019 and the problem resolved itself for me locally. No changes required. – dev'd Dec 15 '21 at 15:37
9

For us, a simple clean & rebuild resolved the issue, without changing any other settings or configurations.

Zimano
  • 1,870
  • 2
  • 23
  • 41
3

Update the Microsoft.Net.Compilers nuget package.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    Please explain what additional insight your post provides on top of the answer https://stackoverflow.com/a/65643330/7733418 Or delete this please. – Yunnosch Mar 31 '22 at 17:51
1

All of the projects created in VS, there is a property under Build tab named "Platform target" which has some dedicated value like x64, any and x86. When you open instance of VS for a particular solution, default in top menubar is "any cpu" which makes a conflict. Make sure you have common config everywhere, this will give you a good build.

0

in case anyone encountering a similar error:

Project-->Properties-->Build-->Advanced-->then Change Language Version like this: [enter image description here

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
Abdou.kou
  • 1
  • 1
0

I had similar issue. Installing the .Net Framework 4.8 targeting package solved the problem.

  1. go to VS installer
  2. click Modify
  3. in Individual components, check .Net Framework 4.8 targeting package
  4. install
  5. open your solution and build it