C# – MethodAccessException inside System.Web.WebPages migrating mvc 3 to mvc 5

asp.net-mvcasp.net-mvc-5c

Using Visual Studio 2013, I migrated a hybrid Asp.Net Webforms/MVC 3/Framework 4.0 web application to Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1.

I followed the steps outlined in these links:

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/upgrading-signalr-1x-projects-to-20

When I run the application in visual studio I get this error:

Attempt by security transparent method
'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()'
to access security critical method
'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly,
System.String)' failed.

I have tried these suggestions that I found on StackOverflow, but none fix the problem:

  1. clear the bin folder and rebuild
  2. Install Nuget package Microsoft.Aspnet.WebHelpers.
  3. Make sure all web.config references of System.Web.Mvc and System.Web.WebPages point to the correct version (5.2.2.0 and 3.0.0.0 respectively).

Any advice on how to find the cause of this error would be much appreciated.

The full stack trace of the error:

[MethodAccessException: Attempt by security transparent method 'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()' to access security critical method 'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)' failed.]
   System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule() +96
   System.Web.WebPages.Administration.PreApplicationStartCode.Start() +41

[InvalidOperationException: The pre-application start initialization method Start on type System.Web.WebPages.Administration.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()' to access security critical method 'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)' failed..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +559
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): The pre-application start initialization method Start on type System.Web.WebPages.Administration.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()' to access security critical method 'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)' failed..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Best Answer

The problem was that the web application had a reference to a (Framework 1) version of System.Web.Administration.dll, I assume because this reference was needed a couple of years ago when the application moved from web forms to hybrid webforms/MVC 3.

I removed this reference and the problem disappeared. Apparently this dll is no longer used. I assume the runtime was fetching the conflicting version of System.Web.Administration from this dll instead of using the new version.