Fixed: Error when deploying a .Net 4.0 project (that was downgraded from .Net 4.5) to a .Net 4.0 server

25 Jun 2013 .Net, Server 2008, Visual Studio No Comments

I recently had to downgrade a .Net 4.5 project to .Net 4.0 so that it could run on MS Server 2003. I had many warnings and errors which I managed to clear up, as detailed in my previous post Downgrading an MVC4 project from .Net 4.5 to .Net 4.0, and the project built (hurrah!). Then I deployed it to the 2003 server and received the following error:

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=, 
Culture=neutral, PublicKeyToken=b77a5c561934e089'.


Googling this error did not show anything useful as others seemed to be having problems going from .Net 4.0 to .Net 4.5 (the solution for most people seemed to be to install .Net 4.5 on their server, but this was not an option for me as my server is running MS Server 2003), but this did give me the clue that it probably was a .Net version problem..

I used JustDecompile (a free program by Telerik) to look at all the dlls in my bin directory (of the published project) and low and behold there were a few that were still compiled against .Net 4.5.

Now to get these DLLs to target .Net 4.0…

First I looked at NuGet Package Manager thinking I could uninstall and reinstall the affected packages, but the fact that they all had various dependencies on each other meant this was not practicable.

Luckily my next endeavour turned out to be the answer. I looked at packages.config in my project root folder (same folder as the .csproj file) and opened it in a text editor (Notepad++ is my preference). Look at all those 45 references!

This is what is looked like:

<?xml version="1.0" encoding="utf-8"?>
     <package id="DotNetOpenAuth.AspNet" version="" targetFramework="net45" />
     ...various other packages, most trageting 40 and some 45...

So I just changed all the net45 references to net40, saved the file and rebuilt my project.

<?xml version="1.0" encoding="utf-8"?>
     <package id="DotNetOpenAuth.AspNet" version="" targetFramework="net40" />

Looking at the offending DLLs in the bin directory, with JustDecompile, confirmed that they were now targeting .Net 4.0 (Hurrah!).

And now it runs on my 2003 server :)