Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MethodAccessException inside System.Web.WebPages migrating mvc 3 to mvc 5

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
like image 366
Tom Regan Avatar asked Sep 10 '14 21:09

Tom Regan


3 Answers

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.

like image 143
Tom Regan Avatar answered Oct 22 '22 15:10

Tom Regan


I was getting the same error on Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1. when I published to Azure (it was working fine on localhost).

The solution, obtained from http://craigwilson.id.au/2014/03/18/azurewap-system-methodaccessexception-attempt-by-security-transparent-method/ was:

  1. Install the package Microsoft.AspNet.WebHelpers. From the Package management console run the following command:

    Install-Package -Id Microsoft.AspNet.WebHelpers

  2. Check the deployment option "Remove additional files at destination" located at Publish... -> Settings -> File Publish Options.

like image 7
Alberto Martin Avatar answered Oct 22 '22 16:10

Alberto Martin


When I had this problem I also had to delete the contents of the obj folder.

like image 2
Vern D. Avatar answered Oct 22 '22 16:10

Vern D.