This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct.

The first line of all views get highlighted and when you hover over them you get this error

The pre-application start initialisation method Run on type WebActivator.ActivationManager threw an exception with the following error message Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

NB: if anyone has the same symptoms on output from your build server check that you have the .net 4.0 reference assemblies, after installing .net 4.5 you'll need to copy them from your dev box. These are typically somewhere like: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 For more details see marcgravell.blogspot.co.nz/2012/09/…
– MysterFeb 25 '13 at 23:06

1

I've just seen a similar issue with a .NET 4.5 DLL being used as a plugin for Microsoft Dynamics CRM 2011 on a machine which only had .NET 4.0. Rather than just rejecting it outright, it registered it and then completely broke workflow customisation (the plugin contained a custom workflow activity). Trace showed that it couldn't find ExtensionAttribute in mscorlib, led me here, rebuilt it for .NET 4.0 and problem solved! Thought that should be mentioned for future Google-fu.
– Matthew WaltonJun 13 '14 at 9:57

10 Answers
10

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly mscorlib

Yes, this technically can go wrong when you execute code on .NET 4.0 instead of .NET 4.5. The attribute was moved from System.Core.dll to mscorlib.dll in .NET 4.5. While that sounds like a rather nasty breaking change in a framework version that is supposed to be 100% compatible, a [TypeForwardedTo] attribute is supposed to make this difference unobservable.

As Murphy would have it, every well intended change like this has at least one failure mode that nobody thought of. This appears to go wrong when ILMerge was used to merge several assemblies into one and that tool was used incorrectly. A good feedback article that describes this breakage is here. It links to a blog post that describes the mistake. It is rather a long article, but if I interpret it correctly then the wrong ILMerge command line option causes this problem:

/targetplatform:"v4,c:\windows\Microsoft.NET\Framework\v4.0.30319"

Which is incorrect. When you install 4.5 on the machine that builds the program then the assemblies in that directory are updated from 4.0 to 4.5 and are no longer suitable to target 4.0. Those assemblies really shouldn't be there anymore but were kept for compat reasons. The proper reference assemblies are the 4.0 reference assemblies, stored elsewhere:

So possible workarounds are to fall back to 4.0 on the build machine, install .NET 4.5 on the target machine and the real fix, to rebuild the project from the provided source code, fixing the ILMerge command.

Do note that this failure mode isn't exclusive to ILMerge, it is just a very common case. Any other scenario where these 4.5 assemblies are used as reference assemblies in a project that targets 4.0 is liable to fail the same way. Judging from other questions, another common failure mode is in build servers that were setup without using a valid VS license. And overlooking that the multi-targeting packs are a free download.

Using the reference assemblies in the c:\program files (x86) subdirectory is a rock hard requirement. Starting at .NET 4.0, already important to avoid accidentally taking a dependency on a class or method that was added in the 4.01, 4.02 and 4.03 releases. But absolutely essential now that 4.5 is released.

I'm getting all the same symptoms, but I'm not using ILMerge, any clues? stacktrace here issues.umbraco.org/issue/U4-1708, it could be a 3rd or 4th party DLL, but how do I find it?
– MysterFeb 21 '13 at 4:03

3

Note: You may not have a "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" folder in case of 64 bit windows versions, in that case check if you have a "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" folder.
– Maarten DocterMay 29 '13 at 10:12

3

I didn't install ILMerge and I have this problem, so how can I fix it? Do I have to install .net 4.5 on the target server?
– TamarGJul 7 '13 at 10:43

4

It is bewildering to me why everybody keeps insisting that this is something that MS should solve. They won't, they can't fix your broken projects or build servers. Use the correct reference assemblies, problem solved.
– Hans PassantFeb 27 '14 at 17:25

I had this problem, except the type it couldn't load was System.Reflection.AssemblyMetadataAttribute. The web application was built on a machine with .NET 4.5 installed (runs fine there), with 4.0 as the target framework, but the error presented itself when it was run on a web server with only 4.0 installed. I then tried it on a web server with 4.5 installed and there was no error. So, as others have said, this is all due to the screwy way Microsoft released 4.5, which basically is an upgrade to (and overwrite of) version 4.0. The System.Reflection assembly references a type that doesn't exist in 4.0 (AssemblyMetadataAttribute) so it will fail if you don't have the new System.Reflection.dll.

You can either install .NET 4.5 on the target web server, or build the application on a machine that does not have 4.5 installed. Far from an ideal resolution.

I had this exact same problem with a site (Kentico CMS), starting development in 4.5, finding out the production server only supports 4.0, tried going back down to target framework of 4.0. Compiling the other posts in this thread (specifically changing target framework to .Net 4 and .Net 4.5 still being referenced). I searched through my solution and found that a handful of the NuGet packages were still using libraries with targetFramework="net45".

I just ran into this annoying problem today. We use SmartAssembly to pack/obfuscate our .NET assemblies, but suddenly the final product wasn't working on our test systems. I didn't even think I had .NET 4.5, but apparently something installed it about a month ago.

I uninstalled 4.5 and reinstalled 4.0, and now everything is working again. Not too impressed with having blown an afternoon on this.

Advance warning for you, EVEN IF you resolve the issue otherwise, your obfuscated version will be broken again, so you may never know you resolved it. That is, you CAN build on 4.5 and deploy without trouble to 4.0-only machines. All that was necessary for me was the multi-targeting patch Hans Passant mentions. I could see from looking at the manifest in ILDASM that it was correctly targeting System.Core instead of mscorlib. But NOT on the version that had been run through SmartAssembly (v5.5).
– Josh SutterfieldJan 23 '15 at 21:06

I did encounter the same problem while trying to read data from a Firebird Database.
After many hours of searching, I found out that the problem was caused by an error I made in the query.
Fixing it made it work perfectly. It had nothing to do with the version of the Framework

We ran into this problem and tracked it down to the Geocoding.net NuGet package that we were using to help with our Google Maps views (Geocoding.net version 3.1.0 published 2/4/2014).

The Geocoding dll appears to be .Net 4.0 when you examine the package file or view it using Jet Brains’ Dot Peek application; however, a colleague of mine says that it was compiled using ilmerge so it is most likely related to the ilmerge problems listed above.

It was a long process to track it down. We fetched different changesets from TFS till we narrowed it down to the changeset that added the aforementioned NuGet package. After removing it, we were able to deploy to our .NET 4 server.

Just adding this answer to help Google save some punter the hours I have spent to get here. I used ILMerge on my .Net 4.0 project, without the /targetplatform option set, assuming it would be detected correctly from my main assembly. I then had complaints from users only on Windows XP aka WinXP. This now makes sense as XP will never have > .Net 4.0 installed whereas most newer OSes will. So if your XP users are having issues, see the fixes above.

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).