Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform

We just had this error in one of our test environments. The problem was in the web.config file. The section had settings to reference the DotNetCompilerPlatform assembly, but had the wrong version number. We updated web.config to the proper version number and it fixed the error.

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

This problem comes from your Nuget package Microsoft.CodeDom.Providers.DotNetCompilerPlatform.

It is the wrong version. You can remove the Current Version then install again from Nuget.


It can also happen if your target folder is something other than "bin/". Roslyn by default looks for the "roslyn" folder in the "bin" folder, so if you are compiling, for example, to "bin/debug/", there may not be a roslyn folder there, and thus it can't find the provider.

You can fix this by reverting to a plain "bin/" target, or by ensuring that a full roslyn folder is copied across to the "bin/debug/" folder as a Post-Build event. Because a publish always pushes to the correct target folder despite the compilation target folder of your build machine, that's why it fails when you run locally, but works if you deploy it.

(Some people have the opposite problem, it working locally, but fails when deployed to IIS, because IIS doesn't have a copy of the CodeDom provider in its GAC, but that's a totally different issue.)