0

Is it possible to use a C# 7 language feature, e.g. 'var mytuple = (1, 2);' in a .NET Framework 4.8 web application (no .NET Core/.NET7) without deploying Roslyn and without precompiling my web application?

Microsoft documentation says .NET Framework 4.8 supports C# 7.3, which led me to believe that is possible. According to Lex Li, the answer is 'No, not possible'. From his blog: "Starting from Visual Studio 2015, the 1.0 release of Roslyn based C# compiler became the default compiler, but Microsoft decided to make it part of Visual Studio (also as NuGet package), instead of being part of .NET Framework."

Seems Microsoft never changed that, and I need to deploy Roslyn or precomile everything or stay on C# 5.

== original question, only for readers who wonder what the discussion in the answers is about ==

In my .NET Framework 4.8 website with ASP.NET pages, I want to use features from C# 7. When I try, I get this compiler error:

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" ... 

Microsoft (R) Visual C# Compiler version 4.8.4084.0 for C# 5 This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5

It then suggests to use Roslyn. Bundling Roslyn within my website works - but unfortunately it causes problems on some older (Windows 2012) servers. I am looking for a solution without bundling Roslyn, without using NuGet to install Roslyn, or similar suggestions. I only want C# 7 support.

  • According to the Microsoft documentation, all versions of .NET Framework support C# 7.3. Installing the latest .NET Framework 4.8.1 runtime did not help. The csc.exe is still the version that only supports up to C# 5.
  • According to this SO issue, C# 7 should work out of the box in ASP.NET. But my page still says the compiler supports up to C# 5.
  • I cannot add a <compiler> tag to Microsoft.CodeDom.Providers.DotNetCompilerPlatform in my web.config, because that is Roslyn and I do not have (or want) a Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll on the server.

Does anyone know if this is possible? Is there an installation that sets the csc.exe in C:\Windows\Microsoft.NET\Framework64 to a version that supports C# 7.3 as advertised?

Casper
  • 53
  • 8
  • "Installing the latest .NET Framework 4.8.1 runtime did not help"--did you try installing the SDK? – StriplingWarrior Apr 24 '23 at 15:02
  • 1
    Probably better off fixing this problem first: "causes problems on some older (Windows 2012) servers". Roslyn on 2012 should run just fine. – mxmissile Apr 24 '23 at 15:03
  • What _exactly_ do you mean by "ASP.NET pages"? Where do you write this C# that gets compiled, and when does it get compiled? Locally or on your server? Do you know [Server 2012 will be out of support in 6 months](https://learn.microsoft.com/en-us/lifecycle/announcements/windows-server-2012-r2-end-of-support)? – CodeCaster Apr 24 '23 at 15:38
  • 2
    https://halfblood.pro/the-rough-history-of-the-so-many-c-compilers-f3a85500707c if you read more, you will see the answer is impossible. Roslyn compilers must be used if you more recent C# features. – Lex Li Apr 24 '23 at 19:55
  • Thanks for the answers. Installing .NET Framework SDK or Developer Pack did not help. The Windows 2012 Server issue is unfortunate, but not the cause of the issue. I updated my question to make that clearer. Lex's reaction seems the right one. – Casper Apr 25 '23 at 08:56
  • Again, _where_ and _when_ do you want to write and compile this code? Is it a Web Site, or a Web Application? If you precompile your (MVC Views / Razor Pages / WebForms pages) you don't have to ship the compiler, you are using it at publish time. – CodeCaster Apr 25 '23 at 08:57
  • @CodeCaster indeed not everything is precompiled. The web application is huge, and composed of several ASP.NET and ASP.MVC web applications projects all ending up in one site. Precompile everything was something I hoped to avoid, if only that default compiler would play nice. – Casper Apr 25 '23 at 09:04
  • As noted, if you using web site application deployment model, then you don't need Roslyn on the server. If you using the asp.net web site and and NOT "application", then the answer is no. However, MVC does not allow nor have such a deployment model, so I can't see how that would be a issue. However for asp.net webforms, then as noted, if you use a web site application, then no Roslyn is required on the server. If you are deploying using a "web site" as NOT an application, then YES you need Roslyn since IIS will and is and can compile code. See my edit to my answer below. – Albert D. Kallal Apr 25 '23 at 16:31
  • So, the answer = yes, assuming you using a asp.net application deployment model. And since you suggest that your using MVC, then you don't have a choice anyway!!! – Albert D. Kallal Apr 25 '23 at 16:37
  • Rather than adding "== edit ==" to your question, just change your question so that it is clear from first reading, without having to read the extra text. – Heretic Monkey Apr 25 '23 at 16:39
  • Edited again, now with the question on top. I kept the original question, maybe it will help someone one day when searching on the error message or other terms. Also made it clear that precompile is not what I was looking for in the question. – Casper Apr 28 '23 at 09:29

1 Answers1

-1

Remember, Roslyn occurs at the source code level, and STILL spits out .net framework code.

in fact, the REAL trick here is to NEVER let IIS compile your code! As you pointed out, some servers don't like having Roslyn installed. But it can get MUCH worse when you attempt to deploy to some hosting service. (they often don't even allow you to post + install .exe files anyway.

And next up?

Your deployment model and type of project matters HUGE here!!!

Are you deploying a web site, or a web site application?

While a web site application is IMHO a better choice?

When you deploy, all source code is stripped out for pages + code behind and compiled into a .dll. However, App_code is a exception!!!! If you look close, you see/note/realize that app_code DOES INCLUDE the source code!!!

What that means? If your target server does not have Roslyn installed, or Roslyn support?

Then you will find AFTER deployment, some code can break!!

The reason is that EVEN when you deploy your code as pre-compiled? IIS will STILL in some cases attempt to compile code in App_code.

Solution:

Don't use app_code!!!!

All I do is re-name App_Code (actually I just leave it alone), and move all code modules OUT of App_Code into a folder called MyAppAcode.

Just remember now that as you drop in (or create) new code modules in MyAppcode, make sure you set the build option of that class or module to "compile". (so for each file you add to MyAppcode, right click on properties, and select build action = compile.

The end result?

You can now publish your application, and it will have been built + compiled local on your dev box, and done so BEFORE deploy. This also means that then Roslyn compiler extensions are NOT required on the target server.

So, Roslyn is not required here, but you MUST adopt a web site application deployment model - the web site option will not work, since then IIS will be compiling code on the server. With a web site "application", no source code is deployed to the server, and thus no on-server of compiling source code occurs. (so, a re-name of app_code will solve this issue).

You don't need those Roslyn extensions on the server (IIS) if you remove the choice from IIS to compile your code!

In other words, assuming this:

Don't use app_code anymore, we use our own folder called MyAppCode.

And we are using the web site application deployment model, NOT using web site model, then we are fine anyway.

In other words, Roslyn should not matter if you deploy using a web site application, and eliminate the ability and chance of IIS doing any compile of your code for you anyway!

The Roslyn extensions in effect outputs standard .net framework code, and thus as long as you compile with Roslyn before deployment, then you should be ok.

Edit:

However, if you are using a web site and not web site "application", you can't do above, and none of the above advice applies to you. In other words, if possible, use + adopt the web site application model here, and all of the above becomes possible, since we never let IIS compile the source code. And better is no source code has to be deployed on the server anyway.

Edit2: Answer = yes, if using web site application

So, if you grasped all of the above? Then the result of above information is YES you can deploy to a web site without Roslyn WHEN you use a web site "application" as opposed to a web site. The reason of course is that/this deployment option pre-compiles your code on your developer computer, BEFORE deployment, and as a result, the only requirement on the web server is the correct .net runtime, but no requirement for the Roslyn compiler on that server exists, since we are NOT compiling code on the server and we are NOT allowing IIS to do the compile of code on the server.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Where did you read .NET Core? – CodeCaster Apr 24 '23 at 15:37
  • I assumed he meant .net 7, which of course is .net core. However, turns out, EVEN if we ignore my mistake (I'll fix that), the advice about Roslyn is still 100% correct and relevant anyway. But, yes, looking at the post, it looks to be c#7, and NOT .net core as I had assumed here. So, I'll edit that part about consuming .net core code - the rest of the post remains as is and is correct advice. – Albert D. Kallal Apr 24 '23 at 17:01
  • You assume App_Code (also not mentioned), there still might be something else at play (compiled WebForms pages or Razor pages). – CodeCaster Apr 24 '23 at 17:24
  • The poster notes quite older servers, and does not mention or note Razor pages. (in fact poster does not even mention MVC). We can both speculate here, but without additional information, then we have to assume a standard setup, and give "general" advice as such. So far those "reasonable" assumptions are holding up rather well. Without more info, we can't assume Razor pages, or even MVC pages. We don't know that. If poster comes back with more info then we will know! So far my advice stands as such until we can make those additional assumptions, or we assume poster is happy with my advice. – Albert D. Kallal Apr 24 '23 at 17:48
  • 1
    The way this site works is that when a question is clear, it gets a definitive answer. Until that time, we ask clarification using comments. – CodeCaster Apr 24 '23 at 19:16
  • that is the way you work - I make assumptons to answer questions, and we all do, the difference is I fully admit as such. And since the poster has not come back, then so far my assumptions looks to be valid and correct, and that results in a better answer here then anyone else has offered. If additional information comes up, then I can simple change my post then, right? but, gooly gee, so far I don't have to change anything, do I? funny how that works, right? – Albert D. Kallal Apr 24 '23 at 19:31
  • 1
    @CodeCaster you can find better use of your time. – Lex Li Apr 24 '23 at 19:58
  • Thanks for taking the time to go through my question and suggest solutions. I edited it to make it clearer. Seems the answer is 'no'.. – Casper Apr 25 '23 at 08:57
  • Answer is yes and the server doesn't need Roslin extensions WHEN you deploy a web site application. So that's quite much what my whole Answer means and says. – Albert D. Kallal Apr 25 '23 at 14:19
  • Rather than adding "Edit" to your post, just change your post so that it is clear from first reading, without having to read the extra text. Readers can view the edit history if they really want to know what changed. Remember; posts are for future viewers, not just the OP. – Heretic Monkey Apr 25 '23 at 16:41
  • Well, the first part of the text explains the why and how. In other words that all important explain is the difference in telling you 2 + 2 = 4 as opposed to sharing with you how to add the two numbers. With that information, I was VERY surprised the poster could not grasp THEN the answer on their own. This is the difference between great people in society that share their knowledge and wisdom as opposed to those that tell the answer without a explain. Giving an answer yes or simple no, is of no value. the information I provide here? It will last you a lifetime and bear fruit for years. – Albert D. Kallal Apr 25 '23 at 16:47
  • In my case, the entire web application is composed of multiple solutions, based on SPA, ASP.NET web application, ASP.MVC 3 web application and others. It is not possible to deploy all that without some pages relying on dynamic compilation. I added that to the main question, so hopefully this issue can now help readers if both situations. – Casper Apr 28 '23 at 09:44
  • Well, at least for web forms, you can remove the server side IIS need to compile pages. I though that MVC allows the same. I am of the view to NEVER let the web server do the compile of code, but that of the developers doing that part. So, adopting a typical software model in which the developers compile and deploy code is of course the best road to take here (assuming one can take that road). Thus, adopting a build + compile model takes the Roslyn issue off the table. If you deploying source code, then you have no choice but to disable Roslyn extensions, or make sure IIS + server has Roslyn. – Albert D. Kallal Apr 28 '23 at 15:30