How do I sync the background compiler version with the compiler that runs when I build a website?

Viewed 32

I'm using the VS2019 IDE. I have a solution with various class library projects and a website. The website is not in a csproj file; it's a locally hosted IIS website and VS2019 lists it as http://localhost/websitename/ in Solution Explorer. Everything is targeting .NET Framework 4.7.2. The website has the Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0 NuGet package installed.

When using a newer language feature like the abbreviated new operator in this example:

StringBuilder sb = new();

...the class libraries compile just fine. Within the website, the background compiler doesn't complain about this, either. But if I build the website, I get:

CS8124: Tuple must contain at least two elements.
CS1526: A new expression requires (), [], or {} after type

If I change the code back to the classic way:

StringBuilder sb = new StringBuilder();

...the website builds fine, but the background compiler gives the following "message":

IDE0090 'new' expression can be simplified

The csproj files for the class libraries all contain:

<LangVersion>latest</LangVersion>

...and the web.config contains:

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

What am I missing?

PS: I don't necessarily need the latest language features. I'd be fine if the code just compiled the same way between the background compiler and the one that's used when VS does builds.

1 Answers

Ok, it looks like you have Rosyln enabled for development, but those extensions are not setup and deployed on the web server.

There are several solutions - which is best for you will depend on if this is a asp.net web site, or a asp.net web site applcation.

So, you could remove + dump roslyn from your project.

Another way, is if you using an asp.net site applcation, then the site is pre-compiled by VS and NOT by the web server. I perfer this option, since then you get FAR BETTER resoltuion when refercing external assemblies and classes.

However, the HUGE down side? To change ONE line of code behind, then you have to do a full re-publish of the site.

However, as noted, I find the benefits FAR outwiegh the down sides.

So, one choice you can make is to use a asp.net web applcation, and deploy as a application.

However, while all code is pre-compiled BEFORE publish to the web server? the one area hat is NOT pre-compiled - and WILL be attempted to be compiled by IIS (the web server) is the App_code folder.

So, what I do is create a new "genreal" folder that holds your routnes and call it say MyCode. Then for the code modules (vb.net) or static classes (c#), you simple right click on those files->properties, and ensure that "build action" is compile.

eg like this:

enter image description here

So in above, you can see in place of App_Code, I created a new folder called MyApp_Code.

As a result, then IIS does not attempt and will not compile that code when I publish.

(in fact, the code and folder will not even exist after a publish).

As I stated, the above suggestion only works if you using a asp.net web site application and a "sln" project file. If you using an asp.net web site? Then you have to either get IIS setup to have the rosyln compiler extensions setup on the server, or simple dump (remove) the rosynln compiler extensions from your project.

By default (if my memory is correct), you see/find the roslyn extensions installed in your project via nuget.

How to remove the compiler extensions is outlined here:

https://www.c-sharpcorner.com/Blogs/remove-roslyn-support-from-asp-net-project

So, as noted, one of the above two choices will work. Due to building and using a lot of external library code, being able to global reference many classes etc., and having the site pre-compile before deploy, then using + adopting a sln and asp.net web site applcation is a FAR BETTER deployment model and coding model then using "just" a asp.net web site. However, some STILL prefer the asp.net web site approach, since you can modify one page, and even just one line of code (code behind), and deploy that one page, and you are done.

Some even open the production site directly. And a simple change to a aspx page can be done by hitting ctrl-s. With the application deploy model, you lose this easy deploy if you go with a "applcation", but for most, the benefits still outweigh the difficulty in changing code and THEN having to do a full re-publish of the site.

I don't have a handy reference as to how to install Roslyn support on IIS, and if you using a hosting provider, then you probably don't have that choice + option anyway.

So, in that case, you either remove Roslyn support from your project, or you adopt the web site applcation publish model. (and move any code in App_Code to another folder to force pre-compile of such code before you deploy.

Related