Question

I recently upgraded to ASP.Net MVC 5 and upon upgrading my code no longer runs.

I'm performing plugin loading via reflecting on types in my current AppDomain.

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
IEnumerable<Type> types = assemblies.SelectMany(o => o.GetTypes()).ToList(); // <-- Throws error

I'm getting the error on the Microsoft.Web.Mvc assembly:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

and upon retrieving the loader exceptions I get the following:

  • [0] {System.TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.Web.Mvc.CreditCardAttribute.GetClientValidationRules(System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.} System.Exception {System.TypeLoadException}
  • [1] {System.TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.Web.Mvc.EmailAddressAttribute.GetClientValidationRules(System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.} System.Exception {System.TypeLoadException}
  • [2] {System.TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.Web.Mvc.FileExtensionsAttribute.GetClientValidationRules(System.Web.Mvc.ModelMetadata, System.Web.Mvc.ControllerContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.} System.Exception {System.TypeLoadException}
  • [3] {System.TypeLoadException: Inheritance security rules violated by type: 'Microsoft.Web.Mvc.CookieValueProviderFactory'. Derived types must either match the security accessibility of the base type or be less accessible.} System.Exception {System.TypeLoadException}
  • Etc...

I know the MVC team removed the AllowPartialTrustedCallers assembly attribute. This is a breaking change http://www.asp.net/visual-studio/overview/2013/release-notes#knownissues . My web.config is set to full trust, but still no-go.

Any ideas??

Was it helpful?

Solution

It looks like you are using some MVC 4 DLLs.

Note, as a side effect of this you cannot use 4.0 and 5.0 assemblies in the same application. You need to update all of them to 5.0.

Likely you need to look and see which assemblies are trying to be loaded as 4 versions.

The reason this is a problem is the MVC 4 assemblies are attributed with AllowPartialTrustedCallers, however the MVC 5 assemblies do not.

Since the MVC 5 DLL has a base class for the MVC 4 DLL, the MVC 4 DLL needs to have the same or more restrictive permissions on its classes. Since it does not (which is why this was a breaking change) the security system fails.

Also note that this is a runtime error partially due to the fact that the built versions did not have this issue (you are effectively swapping in a new base class).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top