【问题标题】:MethodAccessException inside System.Web.WebPages migrating mvc 3 to mvc 5System.Web.WebPages 中的 MethodAccessException 将 mvc 3 迁移到 mvc 5
【发布时间】:2014-09-10 21:16:58
【问题描述】:

使用 Visual Studio 2013,我将混合 Asp.Net Webforms/MVC 3/Framework 4.0 Web 应用程序迁移到 Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1。

我按照这些链接中列出的步骤进行操作:

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

当我在 Visual Studio 中运行应用程序时,出现此错误:

通过安全透明方法尝试 'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()' 访问安全关键方法 'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)' 失败。

我已经尝试了我在 StackOverflow 上找到的这些建议,但没有一个能解决问题:

  1. 清除 bin 文件夹并重建
  2. 安装 Nuget 包 Microsoft.Aspnet.WebHelpers。
  3. 确保 System.Web.Mvc 和 System.Web.WebPages 的所有 web.config 引用都指向正确的版本(分别为 5.2.2.0 和 3.0.0.0)。

任何关于如何找到此错误原因的建议将不胜感激。

错误的完整堆栈跟踪:

[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

【问题讨论】:

  • 你试过安装 Microsoft.AspNet.WebPages.Data 包吗?
  • 是的,Microsoft.AspNet.WebPages.Data 包已安装。

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

问题在于 Web 应用程序引用了 System.Web.Administration.dll 的(框架 1)版本,我认为是因为几年前应用程序从 Web 表单迁移到混合表单时需要此引用网络表单/MVC 3.

我删除了这个引用,问题就消失了。显然这个 dll 不再使用了。我假设运行时从这个 dll 中获取 System.Web.Administration 的冲突版本,而不是使用新版本。

【讨论】:

  • 这个答案为我节省了几个小时。谢谢
  • 你是如何删除引用的?
  • @e4rthdog,在解决方案资源管理器中展开“References”节点,找到System.Web.Administration.dll,右键,选择“remove”
  • 我遇到了大致相同的错误,我必须删除的引用是 System.Web.WebPages.Administration。请注意,它是“.WebPages”。这与上面的不同。
  • 不同的 DLL 是否会导致相同的错误?强制更新依赖项后我有同样的错误,但我找不到罪魁祸首。您将如何查找框架不匹配?
【解决方案2】:

我在 Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1 上遇到了同样的错误。当我发布到 Azure 时(在 localhost 上运行良好)。

http://craigwilson.id.au/2014/03/18/azurewap-system-methodaccessexception-attempt-by-security-transparent-method/ 获得的解决方案是:

  1. 安装包 Microsoft.AspNet.WebHelpers。从包管理控制台运行以下命令:

    Install-Package -Id Microsoft.AspNet.WebHelpers

  2. 检查位于发布... -> 设置 -> 文件发布选项的部署选项“在目标位置删除其他文件”。

【讨论】:

  • “在目标位置删除其他文件”是导致我出现问题的原因。我认为默认 Azure ASP.NET Web 应用程序中有一些较旧的 DLL 需要删除。
【解决方案3】:

当我遇到这个问题时,我还不得不删除 obj 文件夹的内容。

【讨论】:

  • 感谢您的建议,但删除 obj 文件夹并不能解决问题。我已经删除了 obj、bin 以及框架临时 ASP.net 文件,甚至尝试重新启动。
【解决方案4】:

我从模板创建了一个新网站,当网站发布到 Azure 时出现此错误。我已经检查了默认情况下未选中的“删除额外文件”和“预编译”,它有所帮助。我没有尝试任何其他选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2015-01-04
    • 1970-01-01
    • 2017-04-24
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多