Enabling c# 7 in a asp.net application
Asked Answered
G

6

60

I just started working on my old solution in Visual Studio 2017. Just opening the solution in the old IDE worked seamlessly. The c# application projects now default to the c# 7.0 compiler. The property pages of those project (compilation/advanced) let easily chose the targeted language version of the compiler, defaulting to the latest.

I cannot find a way to enable c# 7.0 in the asp.net web projects though. If I write a statement such as:

if (int.TryParse("1", out int myInt)) { ... }

the IDE warns me saying that I need to use version 7+ of the language.

My research on this topic shows I should target the specific c# version in the system.codedom compilers area of the web.config file, so to target the newest Roslyn version.

What I have now is:

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>

which targets c# 6. What are the correct settings for c# 7, provided that I have already downloaded the latest Roslyn with nuget?

Update Here is a screenshot of the available Compile options for a web project (it is Italian VS2017 but it should be easy to understand). No possibility to select the targeted c# version there.

Compile options

Graf answered 12/3, 2017 at 6:59 Comment(8)
did you try changing compilerOptions ? msdn documentation state it affects just that. msdn.microsoft.com/en-us/library/f4ckecs0.aspxCoronet
I tried setting it to /langversion:7. Does not compile.Graf
It returns: CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.Graf
in C#6 microsoft added a submenu to the visual studio named "Project" > "Enable C#6" check if they did the same with vs2017, blogs.msdn.microsoft.com/webdev/2015/12/07/…Coronet
No, there is not such item in the Project menu for web applications. If they put this option anywhere, it is hidden very well.Graf
How does asp.net get its compiler? If it gets it through a nuget package, you probably need to update the package reference (to some 2.0.x version).Athapaskan
Julien, I wish Microsoft provided easier tools, but now they have two packages available through nuget: Microsoft.Net.Compilers, which has just been updated to v2.0.1, and Microsoft.CodeDom.Providers.DotNetCompilerPlatform compiler as a service (Roslyn), which is in v1.0.3 (released last December). Should I assume that c# 7 is not yet ready for asp.net?Graf
Note: In order to publish you will also have to add <LangVersion>latest</LangVersion> to your .pubxml fileArrange
R
83

In website's NuGet window:

  1. Uninstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  2. Re-install it
  3. In web.config, under: system.codedom > compilers > compiler, change compilerOptions="/langversion:6 to 7
Reginiaregiomontanus answered 13/3, 2017 at 12:54 Comment(6)
This absolutely works and is the answer! But for completeness sakes for those come here, the OP said assuming latest rosyln installed with nuget. That means installing Microsoft.Net.Compilers 2.0<=. Also if you want to use C# 7 tuples you need to install from nuget System.ValueTupleAleece
None of these suggestions work for me - not this or previous answer.Receptor
There is new version of Microsoft.CodeDom.Providers.DotNetCompilerPlatform (1.0.4) which enables C# 7 on web project. The project has to target to 4.6+, since Roslyn compiler 2.0 need .net framework 4.6.Spermatid
This helps me to compile, but then I get error Could not find a part of the path … bin\roslyn\csc.exe. And this is solutionTaimi
Also possible: /langversion:latest, which is similar to /langversion:default. See documentation).Winnie
be sure and restart visual studio after making this changeLagrange
W
27

I am able to compile it with default language setting but not with C# 7 option. enter image description here

But below setting gives compile time error:

enter image description here

so you can keep your language version setting as default.

If you experimenting with Roslyn and not using Visual 2017 default compiler build then you may need to make some more changes

Select your project name and right click >> Properties Window >> Build and then add the below two options in "Conditional Compilation symbols" text box __DEMO__,__DEMO_EXPERIMENTAL__

enter image description here

Update

In order to use C# 7.0, you need to use 2.0+ version of Microsoft.Net.Compilers

enter image description here

after installing the latest version of Microsoft.Net.Compilers (2.0+) you can select the language version as C# 7.

so the best solution is to install the latest version of Microsoft.Net.Compilers (2.0+).

Wise answered 13/3, 2017 at 5:54 Comment(8)
Banketeshvar, the "Advanced Build Settings" dialog is available only for desktop and console applications. Not for web projects. In web projects, you have to manually edit the web.config file. I have downloaded version 2 of the compilers, but setting /langversion:7 still yelds to the compiler error.Graf
@davidthegrey, I do not think that language version has to do anything with the console or web App. I am able to open Advanced Build setting option with ASP.NET MVC App (both Native framework and .NET Core). I have compiled ASp.NET Core Web App as well with C# 7.0 language setting option.Wise
Are you using the latest version of Visual Studio 2017? I have downloaded the latest version of Visual Studio 2017 Enterprise which was released on 7th march 2017. for ASP.NET Core Web Application I have set the language version using the following steps: Right Click on ASP.NET Core Project --> Select properties from Context menu --> Go to Build Tab --> Click on "Advanced..." button --> It will open the "Advanced Build Setting" Window. The same thing also working for me for ASP.NET MVC projects with .NET Native framework.Wise
I think you must be aware that we need to install compilers for each project separately from Nuget. Could you please give some more details that what type of project you have created earlier with which version of visual studio and now which edition of Visual Studio 2017 you are using. I want to know the preceding info to replicate the issue at my end.Wise
I am on Visual Studio 2017 Professional RTM. I have added a screenshot of the available Compile options for web projects in my original post. And yes, I am aware that every project in the solution use independent compiler settings.Graf
I am using English version of Visual Studio 2017 enterprise and it is displaying me the advanced build setting options. on 7th march 2017 march Microsoft has released the updated version of Visual studio 2017 which have hundred of bug fixes. you can try that installation. visualstudio.com/vs/whatsnewWise
I am using the same Visual Studio version as you, only Professional instead of enterprise and Italian instead of English, but I am sure that the options are the same. We are simply talking about different type of projects. I can see the options you mention in other types of applications inside my solution, but not in the web projects. Anyway, I simply had to uninstall and reinstall the CodeDom providers, as Hassan Abdullah suggested below, which was not obvious at all. Now c#7 works!Graf
This works fine in MVC apps - just not in website apps.Receptor
P
8

If you try to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform version 2.0.0 and your project targets a version of .net older than 4.6, then it will automatically use an older version of roslyn which only supports up to langversion 6. This is because newer versions of roslyn, including the first version to support csharp-7, require at least .net-4.6 to run. If your project targets an older version of .net, you will get the error message you see:

CS1617 Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.

  1. Ensure that your project is targeting at least .net-4.6. Retarget if necessary.
  2. If your project still uses packages.config, then you must uninstall and reinstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform to update your project file to point to the .net-4.6 variant of the nuget package. If you are using <PackageReference/>, you are all set (but you have to manually configure web.config’s system.codedom section).
Parlin answered 12/9, 2018 at 6:21 Comment(1)
Note: Microsoft.CodeDom.Providers.DotNetCompilerPlatform used to take a dependency on Microsoft.Net.Compilers so if you're upgrading an old project, make sure both packages are update or remove Microsoft.Net.Compilersif you don't need it anymore.Arrange
A
6

For C# 7.x support set the Build Configuration Language Version of project to C# latest minor version (latest)

Build Configuration Language Version

If you are using CodeDOM Providers for .NET Compiler Platform ("Roslyn") (e.g. the Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget package) set compilerOptions="/langversion:latest" in web.config for asp.net.

<system.codedom>
   <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:latest /nowarn:1659;1699;1701"/>
   </compilers>
</system.codedom>

For more info:

Argueta answered 27/9, 2017 at 22:57 Comment(0)
D
1

You need to replace files in your project folder

/Bin/roslyn 

with files from NuGet package folder

/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0/tools/Roslyn472

csv.exe in project folder not replaced during installation a new version of nuget. After replacing files it works excellent.

And do not forget to change .Net version to latest in project properties.

Danielldaniella answered 19/9, 2020 at 7:39 Comment(1)
+1 That was exactly the bit of information I was looking for, thank you for taking the time to post that! So... how did you figure that out? I haven't really found any clear guidance on setting up a new asp.net website/4.8 Framework based to use the latest c# version...Roberge
N
0

I was referencing a custom Project A that was referencing another custom Project B. I just readded the references from A to B and it seemed to work (for now).

Narrow answered 21/5, 2019 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.