C# System.MethodAccessException:尝试通过安全透明方法访问所有应用程序的安全关键方法失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12092435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.MethodAccessException: Attempt by security transparent method to access security critical method fails on all applications
提问by Christian
Hello and thanks in advance for the help, I know that this question or similar questions have been posted, frequently with specific regards to MVC 3 applications. However, I am getting this error message whenever I try and launch any application from within visual web developer express 2010 using a .net 4.0 target framework.
您好,在此先感谢您的帮助,我知道这个问题或类似问题已经发布,经常与 MVC 3 应用程序相关。但是,每当我尝试使用 .net 4.0 目标框架从visual web developer express 2010 中启动任何应用程序时,都会收到此错误消息。
The exact error message is:
确切的错误消息是:
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(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 程序集中的所有方法都变为安全透明,这可能是导致此异常的原因。
Stack trace:
堆栈跟踪:
[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
I am completely at a loss here. If I create just any basic application from scratch and try and launch it, even with nothing added, I get this error screen. I have tried removing the target framework="4.0" attribute from the
我在这里完全不知所措。如果我从头开始创建任何基本应用程序并尝试启动它,即使没有添加任何内容,我也会收到此错误屏幕。我曾尝试从
<compilation debug="true" targetFramework="4.0" />
tag and running it with no results. I have tried removing that attribute and adding
标记并运行它,但没有结果。我尝试删除该属性并添加
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0.30319"/>
</startup>
as suggested in this thread: BadImageFormatException - Is it possible to specify which .NET version is used to run an executable?I tried repairing the .Net 4.0 framework installation because I heard that may be an issue. I just installed visual web developer last night in order to work on a project that I moved over from Microsoft Visual Studio 2010 because my trial version expired, any help or potential workarounds would be appreciated.
正如此线程中所建议的:BadImageFormatException - 是否可以指定使用哪个 .NET 版本来运行可执行文件?我尝试修复 .Net 4.0 框架安装,因为我听说这可能是一个问题。我昨晚刚刚安装了 Visual Web Developer,以便处理我从 Microsoft Visual Studio 2010 转移的项目,因为我的试用版已过期,任何帮助或潜在的解决方法将不胜感激。
采纳答案by Drew Noakes
I experienced this issue today. As far as I understand it, the problem is that .NET 4.0 assemblies now default to security critical. It's not possible for security transparent code to call into security critical code.
我今天遇到了这个问题。据我了解,问题是 .NET 4.0 程序集现在默认为security critical. 安全透明代码不可能调用安全关键代码。
The solution in my case was related to having AllowPartiallyTrustedCallersAttributedefined on only a few of the assemblies in my application. I had to either remove this attribute from all assemblies, or add it to all of them.
在我的案例中,解决方案与AllowPartiallyTrustedCallersAttribute在我的应用程序中仅对少数几个程序集进行了定义有关。我必须从所有程序集中删除此属性,或将其添加到所有程序集中。
回答by Christian
Apparently, one potential workaround which at least made it possible for me to launch applications in Visual Web Developer was this:
显然,至少使我可以在 Visual Web Developer 中启动应用程序的一种潜在解决方法是:
Remove the targetFramework="4.0"attribute from the element which is located within the element.
If it is a Silverlight application Right click the ApplicationName.Web header select properties, then change the target framework to 3.5. If it is an asp.net application right click the top project header and select properties and then set the target framework to 3.5.
从位于元素内的元素中删除targetFramework="4.0"属性。
如果是 Silverlight 应用程序 右键单击 ApplicationName.Web 头选择属性,然后将目标框架更改为 3.5。如果是asp.net应用程序,右键单击顶部项目标题并选择属性,然后将目标框架设置为3.5。
All this suggests to me their is a problem with my .Net 4.0 framework installation. But as I have installed and uninstalled and then installed the .Net 4.0 framework at least twice, this is the only option I can think of at this point. If someone provides me a better response/More permanent solution that works for me I'll mark that as the answer. In the meantime I will try launching the "install other frameworks" and reinstalling both the .Net 4.0 targeting pack and .Net 4.0 Runtime once more to see if it works.
所有这些都表明我的 .Net 4.0 框架安装存在问题。但由于我已经安装和卸载,然后至少安装了 .Net 4.0 框架两次,这是我目前能想到的唯一选择。如果有人为我提供了更好的响应/更永久的解决方案,我会将其标记为答案。与此同时,我将尝试启动“安装其他框架”并再次重新安装 .Net 4.0 目标包和 .Net 4.0 运行时,看看它是否有效。
回答by Marcel Balcarek
You may need to add the following to your AssemblyInfo.cs file so that the involved assemblies are the same.
您可能需要将以下内容添加到您的 AssemblyInfo.cs 文件中,以便所涉及的程序集相同。
[assembly: SecurityTransparent()]
回答by Rob
Appreciate I'm extremely late to the party here but in VS2013 and suffering the same issue with a .NET 4.5.1 ClickOnce project, I fixed this issue by:
感谢我在这里参加聚会非常晚,但在 VS2013 中遇到了与 .NET 4.5.1 ClickOnce 项目相同的问题,我通过以下方式解决了这个问题:
- Opening the Project Properties of the ClickOnce project in question
- Opening the Security tab
- Changing the trust settings to "This is a partial trust application"
- Changing the trust settings back to "This is a full trust application"
- 打开有问题的 ClickOnce 项目的项目属性
- 打开安全选项卡
- 将信任设置更改为“这是一个部分信任的应用程序”
- 将信任设置改回“这是一个完全信任的应用程序”
Hope this helps someone else!
希望这对其他人有帮助!

