Enabling C# 7 in a ASP.NET Application

How to use C# 6.0 or 7.0 in an old ASP.NET Website (not Website Project)

If you install the Microsoft.CodeDom.Providers.DotNetCompilerPlatform it will update your web.config file to use the current version - right now it's on 7.3:

  <system.codedom>
<compilers>
<compiler extension=".cs" language="c#;cs;csharp" warningLevel="4"
compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform,
Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4"
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\"Web\" /optionInfer+"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform,
Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</compilers>
</system.codedom>

Using C# 7 features inside of a View in an ASP.NET MVC Core project

Could you try the following (recommended by folks on the ASP.NET core team):

  1. Install the Microsoft.CodeAnalysis.CSharp (version 2.0.0) and System.ValueTuple (version 4.3.0) packages
  2. In Startup.cs, in the ConfigureServices method, configure Razor to use C# 7 by doing the following:

    services.AddMvc().AddRazorOptions(options =>
    options.ParseOptions = new CSharpParseOptions(LanguageVersion.CSharp7));

How can I enable all features of C# 7 in Visual Studio 2017 project?

For arbitrary task-like types you linked to in the 2nd part of your question you need to include the System.Threading.Tasks.Extensions package.

The reason you need these NuGet packages is because the new language features rely on new types added to the .NET framework. The new types that the C# language features depend on will not be "built in to the framework" until the next minor version released after 4.6.2 to not break SemVer1. So unless you are building a project using that next version of the framework you will need to use the NuGet packages to get the features to work.

This is no different than getting extension methods to work in a .NET 2.0 project. You can use extension methods but you need to use a NuGet package (or add the code yourself) to get the types it relies on to be added to your project.


1: So 4.7 or 5.0, whatever they decide to call it, if there is a 4.6.3 it will not be in that version because that is not a minor release version bump, that is a patch version bump and you can't make API changes in a patch version bump without violating Semantic Versioning.

How to use C# 7 within Web Application ASPX code-before pages?

Finally, I solved it.

With the help of Sysinternals Process Monitor I discovered that csc.exe tries to load a file called "csc.exe.config" which was not present.

So I copied the "csc.exe.config" file from my local website's "bin\roslyn" folder into my live website's "bin\roslyn" folder.

After that, everything works as expected.

Just to be safe, I also copied "csi.exe.config" the same way, although it seems that this is not required, at least in my tests.

(The reason that the config files were initially not present is that my publish scripts usually intentionally ignore configuration files to not overwrite existing configurations).

Is C# 7.3 compiler included in .Net Framework 4.8 for asp.net?

The comments are wrong!

Asp.Net websites still use C# 7.0 compiler, despite the fact that I have .Net 4.8 installed!

I took the chance and uninstalled the 'Roslyn' compiler from Github on one of my web sites(link in question). Now I got a lot of errors when compiling.

Example of error (this is a property):

public bool IsReusable => false;

That doesn't work in C# 7.0. So I will have to keep the 'Roslyn' compiler on my websites to get C# 7.3

Update:

For anyone, who have just installed the 'Roslyn' compiler from Github: You must manually set the langversion in Web.config, like this:

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

Now asp.net will use the C# 7.3 compiler.

Enable New Language Features in Visual Studio 2017

The latest version of ReSharper (2016.3) does not have full C# 7.0 support, so I think it's quite possible that's causing the issue.

Your code works fine for me with ReSharper 2017.1 EAP 4, including correctly marking the throw expression as not accessible. So you might want to try upgrading to that, if you're willing to use pre-release software.

Compile errors when using C# 7 features in new VS Studio 2017 ASP.NET MVC 5.2.3 project

Thanks to Panagiotis and Tetsuya. It was the version of c#.

As a reference to this question, you have to do three things:

  1. Uninstall "Microsoft.CodeDom.Providers.DotNetCompilerPlatform" via Nuget Package Manager and reinstall the newest version (currently the 1.0.4)
  2. After this, update Microsoft.Net.Compilers to the newest version (currently 2.2.0)
  3. Change the c# language via project "properties < build < advanced" to version 7.

done.



Related Topics



Leave a reply



Submit