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

Chr*_*ian 18 .net c# asp.net .net-4.0 visual-web-developer-2010

您好,并提前感谢您的帮助,我知道这个问题或类似的问题已经发布,经常对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,版本= 4.0.0.0,文化=中性公钥= 31bf3856ad364e35"标有AllowPartiallyTrustedCallersAttribute程序,并使用2级安全透明度模式.2级透明度导致AllowPartiallyTrustedCallers程序集中的所有方法默认情况下变为透明安全性,这可能是此异常的原因.

堆栈跟踪:

[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
Run Code Online (Sandbox Code Playgroud)

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

<compilation debug="true" targetFramework="4.0" />
Run Code Online (Sandbox Code Playgroud)

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

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v2.0.50727"/>
  <supportedRuntime version="v4.0.30319"/>
 </startup>
Run Code Online (Sandbox Code Playgroud)

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

Dre*_*kes 16

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

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