System.MethodAccessException:尝试通过安全透明方法访问安全性关键方法在所有应用程序上失败

您好,并提前感谢您的帮助,我知道这个问题或类似的问题已经发布,经常与MVC 3应用程序有关。 但是,每当我尝试使用.net 4.0目标框架从visual web developer express 2010中启动任何应用程序时,我都会收到此错误消息。

确切的错误消息是:

尝试使用安全透明方法’System.Runtime.Diagnostics.DiagnosticTrace..ctor(System.String,System.Guid)’来访问安全关键方法’System.Runtime.Diagnostics.EtwDiagnosticTrace..ctor(System.String,System.Guid) )’失败’。

程序集“System.ServiceModel.Internals,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35”标有AllowPartiallyTrustedCallersAttribute,并使用2级安全透明度模型。 2级透明度导致AllowPartiallyTrustedCallers程序集中的所有方法默认情况下变为透明,这可能是此exception的原因。

堆栈跟踪:

[MethodAccessException: Attempt by security transparent method 'System.Runtime.Diagnostics.DiagnosticTrace..ctor(System.String, System.Guid)' to access security critical method 'System.Runtime.Diagnostics.EtwDiagnosticTrace..ctor(System.String, System.Guid)' failed. Assembly 'System.ServiceModel.Internals, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.] System.Runtime.Diagnostics.DiagnosticTrace..ctor(String traceSourceName, Guid etwProviderId) +24 System.ServiceModel.Activation.FxTrace.InitializeTracing() +90 System.ServiceModel.Activation.FxTrace.get_Trace() +84 System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized() +59 System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state) +4 System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state) +52 System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized() +65 System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +58 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +143 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69 

我在这里完全不知所措。 如果我从头开始创建任何基本应用程序并尝试启动它,即使没有添加任何内容,我也会收到此错误屏幕。 我试过从中删除目标framework =“4.0”属性

  

标记并运行它没有结果。 我试过删除该属性并添加

     

正如此线程中所建议的: BadImageFormatException – 是否可以指定用于运行可执行文件的.NET版本? 我尝试修复.Net 4.0框架安装,因为我听说这可能是一个问题。 我刚刚安装了可视化Web开发人员,以便处理我从Microsoft Visual Studio 2010移动的项目,因为我的试用版已过期,任何帮助或潜在的解决方法都将不胜感激。

我今天遇到过这个问题。 据我所知,问题是.NET 4.0程序集现在默认为安全关键 。 安全透明代码无法调用安全关键代码。

我的解决方案涉及只在我的应用程序中的几个程序集上定义AllowPartiallyTrustedCallersAttribute 。 我必须从所有程序集中删除此属性,或将其添加到所有程序集中。

您可能需要将以下内容添加到AssemblyInfo.cs文件中,以使所涉及的程序集相同。

 [assembly: SecurityTransparent()] 

感谢我在这里参加聚会非常晚,但是在VS2013中遇到了与.NET 4.5.1 ClickOnce项目同样的问题,我通过以下方式解决了这个问题:

  1. 打开相关ClickOnce项目的项目属性
  2. 打开“安全”选项卡
  3. 将信任设置更改为“这是部分信任应用程序”
  4. 将信任设置更改回“这是一个完整的信任应用程序”

希望这有助于其他人!

显然,一个潜在的解决方法至少使我能够在Visual Web Developer中启动应用程序是这样的:

  • 从位于元素中的元素中删除targetFramework =“4.0”属性。

  • 如果是Silverlight应用程序,请右键单击ApplicationName.Web标头选择属性,然后将目标框架更改为3.5。 如果是asp.net应用程序,请右键单击顶部项目标题并选择属性,然后将目标框架设置为3.5。

所有这些都告诉我,我的.Net 4.0框架安装存在问题。 但是,由于我已经安装和卸载然后至少安装了两次.Net 4.0框架,这是我现在能想到的唯一选择。 如果有人为我提供了更好的回复/更适合我的永久性解决方案,我会将其标记为答案。 与此同时,我将尝试启动“安装其他框架”并再次重新安装.Net 4.0目标包和.Net 4.0 Runtime以查看它是否有效。