40

I have a webproject I am trying to host, but when the server tries to compile it, I get the following error:

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
--- End of inner exception stack trace --- at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(String[] args)

So the server has an older version of CodeAnalysis than my project. So lets try a binding redirect!

<dependentAssembly>
    <assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-9.9.9.9" newVersion="1.0.0.0" />
</dependentAssembly>

Sadly, I still get the same error. What am I doing wrong?

4

6 Answers 6

41

I figured this out myself. To anyone reading this, the problem was that the Nuget package Microsoft.CodeDom.Providers.DotNetCompilerPlatform (which also contains the CodeAnalysis package) was the wrong version. I created a new webproject, updated the offending Nuget package right away, and the problem was solved.

8
  • 12
    Alternatively, you could just delete the packages folder in explorer, then right the solution in Visual Studio and click "Restore NuGet Packages". Jun 20, 2016 at 0:56
  • 1
    If only you had told me that before I spend 3 hours trying to fix this error. :-)
    – yesman
    Jun 20, 2016 at 4:59
  • 1
    Thank you to you both - it saved me a lot of time.
    – tdracz
    Aug 8, 2016 at 21:43
  • 4
    Also consider manually updating the related assembly version in Web.config: <system.codedom><compilers><compiler ... type="...Version=..." ... />.... This wasn't updated automatically after nuget update of Microsoft.CodeDom.Providers.DotNetCompilerPlatform in my case (VS 2015).
    – stop-cran
    Mar 16, 2017 at 16:31
  • 1
    Awesome !! This saved my day!! Dec 4, 2018 at 5:03
4

Other possible fix (at least for Visual Studio 2015):

Get the latest Visual Studio update.

You can check for updates by going to Tools/Extensions and Updates/Updates

Same as this Q/A: Visual Studio 2015: Quick Actions no longer working, error: Could not load file or assembly Microsoft.CodeAnalysis

0
4

I resolved this issue but ensuring that the version numbers of the assemblies in my web.config were correct.

Edit:

I hit this problem as well and some may wonder how the Web.Config can become incorrect. In my case I am using Web.Config Transforms so the Web.Config file gets overwritten: enter image description here

Changing the version number in Web.Configs compiler (in the Web.Template.Config) to the number matches the NuGet version does solve the issue:

<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0
3

I have had this problem today while doing an Umbraco v7.13.0 upgrade, simply updating the following package has fixed my problem, this is apparently a common problem with things like Umbraco upgrades.

Update-Package -Reinstall Microsoft.CodeAnalysis.CSharp
0

If none of mentioned options work, please go for this answer. Basically, you are registering that Microsoft.CodeAnalysis.dll to GAC

Could not load file or assembly 'Microsoft.CodeAnalysis, version= 1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependenc

-2

Right click on your project >> Manage Nuget packages manager for solution >> Tap on Updates >> Update your 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform' >> and then run your project again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.