7
votes

After watching the key note address from Scott Gu at AspConf I got excited about trying the Windows Azure Web sites with MVC4. I tried deploying an existing MVC application after re-configuring it to target .NET framework 4.0 instead of 4.5 and got the following error.

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

Since I had already added several NuGet packages I thought it might have something to do with Ninject so I created a new MVC project targeting the .NET 4.0 framework with no NuGet packages and deployed to Windows Azure using the Visual Studio 2012 RC Publish option (right-clicking the Web project) and I still get this error.

Does anyone know what's going on or how to fix this issue? Thanks in advance.

Including full stack trace:

[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.]
System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +180
System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +192
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +115
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +426 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) +64
WebActivator.AssemblyExtensions.GetActivationAttributes(Assembly assembly) +64 WebActivator.ActivationManager.RunActivationMethods() +143 WebActivator.ActivationManager.Run() +22

[InvalidOperationException: The pre-application start initialization 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'..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +423
System.Web.Compilation.BuildManager.CallPreStartInitMethods() +306
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +677

[HttpException (0x80004005): The pre-application start initialization 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'..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9090044 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256

3
After re-deploying with a new project I had a different error as outlined int he following post. I tried their solution and it worked for me with a new sample project. I will try again with the 'real' project that has many more NuGet packages installed and verify this solution works. In short check the 'Remove additional files at destination' in the Settings tab of the Publish dialog with a MVC4 project that has been switched to target the .NET 4.0 framework. social.msdn.microsoft.com/forums/en-us/…likestoski
@likestoski - if you have solved the issue, please post as an answer to your own question to help others that may stumble with this same question :)Tommy

3 Answers

1
votes

In the Publish Web Application wizard if I select Settings | Remove additional files at destination this problem went away.

0
votes

It seems to me like a Problem with .NET FX references.

According to the ASP.NET MVC 4 release notes, there's an issue with Entity Framework nuget package and downgrading from .NET 4.5 --> .NET 4. http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253815

Changing an ASP.NET MVC 4 project to target 4.0 from 4.5 does not update the EntityFramework assembly reference: If you change an ASP.NET MVC 4 project to target 4.0 after targetting 4.5 the reference to the EntityFramework assembly will still point to the 4.5 version. To fix this issue uninstall and reinstall the EntityFramework NuGet package.

0
votes

Another possible solution (others with same exception) could be that your build server has not 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 Marc's blog post