29

I have a fairly simple C# WebAPI2 project that runs locally but after publishing to IIS on a remote machine (Windows Server 2012 R2 Standard) the web page displays the following (after setting customErrors to "Off"):

Server Error in '/' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: The compiler failed with error code -2146232576.

If I grab the detailed compiler output and run it on the IIS server, I get a smartscreen error message saying:

This app can't run on your PC. To find a version for your PC, check with the software publisher.

I'm guessing it's something to do with the compiler version but nothing has changed since it last published.

Any ideas?

feganmeister
  • 686
  • 1
  • 8
  • 15
  • Ok, so it turns out something **had** changed. The Newtonsoft.Json package was updated and checked-in without me realising, seems it uplifted the target framework of the project to .NET 4.5.2 from 4. – feganmeister May 10 '17 at 12:49
  • 1
    this could also be caused by `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` and `Microsoft.Net.Compilers packages` Remove them then redeploy and watch your problems fade away :P – Mbithy Mbithy Aug 03 '17 at 21:43

9 Answers9

32

I faced with this problem after upgrade some NuGets and solved with below steps.

Step 1: Remove these NuGet packages from NuGet Package Console

PM> Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
PM> Uninstall-package Microsoft.Net.Compilers

Step 2: Add these system.codedom lines before closing </system.data> tag in Web.config

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

PS: After a while, we removed <system.codedom> completely, for fixed other errors.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Amir Astaneh
  • 2,152
  • 1
  • 20
  • 20
  • 5
    Removing Codedome did the trick for me as well. My project was 4.5.2 but trying to use it. I don't to any code dom compiling so was ok to remove – Juan Jun 21 '17 at 16:12
  • 1
    Removing Nugets and web.config lines did the trick to solve same problem in Azure classic cloud service – cdiazal Jul 10 '17 at 15:23
  • 1
    What's your Roslyn compiler version? If you are using Roslyn compiler 2.0+, you need to install .net framework 4.6+ on your server. – mattfei Aug 04 '17 at 16:24
  • 1
    The problem with this solution is that afterwards you have only C# version 5 left. I tried it and got errors like `"Error CS8026 Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater."` – Matt Nov 07 '17 at 13:52
  • 1
    I did too faced the same situation with same sequence of action i.e. updated nuget packages. I uninstalled those two packages and it automatically removed from my web.config (I did not remove it manually). Thanks – Jay Feb 16 '18 at 09:30
  • This worked for me! Saved me hours ... maybe DAYS trying to figure this out! – Gary O. Stenstrom Sep 06 '19 at 17:17
  • Thank you. The solution helps me to save a lot of time even it am using CS 2022 and the solution still works. – Thomas.Benz Oct 13 '22 at 14:08
27

The culprit is the Microsoft.Net.Compilers package, used to support modern C# syntax/features (version 6.0, 7.0) in your project and in Razor views in particular. Depending on its version, the package requires a particular minimum version of the full .NET framework to be installed on a machine in question.

For instance, the 2.2.0 package requires .NET 4.6+. Even though your project is targeting say .NET 4.5.2, you probably have the latest .NET installed on your development machine, and everything goes just fine. The remote deployment machine only has .NET 4.5.2 installed, and when your ASP.NET application tries to compile resource (e.g. views) at run time, you get error -2146232576.

Your options:

  1. If you absolutely need to use newish C# features in your project - install the latest .NET framework onto the remote machine.

  2. If you are happy with only more or less modern C# features being available - downgrade the Microsoft.Net.Compilers package to a version that only requires a framework you have on your remote machine. For instance, version 1.3.2 only needs .NET 4.5.

  3. If you do not need the said features at all, simply remove Microsoft.Net.Compilers and Microsoft.CodeDom.Providers.DotNetCompilerPlatform (Roslyn CodeDom providers) packages from your project.

Roman Pletnev
  • 5,958
  • 2
  • 21
  • 28
  • Note that if you still want modern C# features being available in code (except views) and the target machine runs on .NET 4.5 you can only remove `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` and keep `Microsoft.Net.Compilers`. – Spotted Dec 04 '18 at 10:03
11

Target framework was changed, had to install .NET 4.6 on the Windows Server.

feganmeister
  • 686
  • 1
  • 8
  • 15
2

for me helped just remove NuGet packages above:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform

Microsoft.Net.Compilers

Recompile and then add them back.

Bladeus
  • 21
  • 1
0

For me, it was because i tried to build a string for the Roles property of the attribute Authorize on my class.

[Authorize(Roles = IdentityRoles.Administrator + "," + IdentityRoles.BaseData)]

I had to build the different strings and just set one constant to Roles:

[Authorize(Roles = IdentityRoles.AutorizationStrings.AdminBaseData)]

where

IdentityRoles.AutorizationStrings.AdminBaseData = "Administrator,Base data";

Hope this helps anybody that tricks themself down this hole.

ekblom
  • 26
  • 1
  • 3
0

I upgraded the .NET framework to version 4.6 and it worked. The Roslyn packages are dependent on versions 4.5+, but it didn't work. So the upgrade made it work.

LeleGaurav
  • 33
  • 1
  • 9
0

I upgraded the NuGet package "Microsoft.CodeDom.Providers.DotNetCompilerPlatform" from 2.0.0 to 2.0.1 and that resolve this issue for me

0

For me, a similar issue was resolved by letting the other user of a server publish the app again. Everytime I published the app, it was depending on a temporary ASP.NET folder that did not exists for the other user. I hope this helps.

0

The best way I know is install .net framework 4.7 on your PC.

Сергей
  • 780
  • 4
  • 13
  • 31