该程序集不允许部分信任的调用者。的InitializeComponent()

时间:2010-11-30 13:47:03

标签: c# .net-3.5 partial-trust initializecomponent

场景:我正在重构我们的一个应用程序以使用Nhibernate,并在几周后遇到了这个问题。这个问题最初是针对Nhibernate和Castle的,为了解决这个问题,他们都是用[assembly: AllowPartiallyTrustedCallers]重新编译的。但是,在对UI和代码库进行一些更改后,此错误再次出现。另外值得注意的是,我以编程方式从Form_Main控制加载我的用户控件。

问题:每当生成用户控件时,我都会收到以下错误。如果我注释掉加载,程序将运行。当我调试它结束于自动生成的InitializeComponent()函数。请注意,我无法进入该功能。

System.Security.SecurityException was unhandled
      Message="That assembly does not allow partially trusted callers."
      Source="A"
      GrantedSet=""
      PermissionState=""
      RefusedSet=""
      Url="file:///C:/Documents and Settings/ID/Desktop/A-NHIB2/bin/Debug/A.EXE"
      StackTrace:
           at A.UserControlCyber.InitializeComponent()
           at A.UserControlCyber..ctor() in C:\Documents and Settings\ID\Desktop\A-NHIB2\UserControl_Cyber.cs:line 34
           at A.FormMain.FormMainLoad(Object sender, EventArgs e) in C:\Documents and Settings\ID\Desktop\A-NHIB2\Form_Main.cs:line 30
           at System.Windows.Forms.Form.OnLoad(EventArgs e)
           at System.Windows.Forms.Form.OnCreateControl()
           at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           at System.Windows.Forms.Control.CreateControl()
           at System.Windows.Forms.Control.WmShowWindow(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WmShowWindow(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
           at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Control.set_Visible(Boolean value)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at A.Program.Main() in C:\Documents and Settings\ID\Desktop\A-NHIB2\Program.cs:line 32
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
           at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
           at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
           at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
           at System.Activator.CreateInstance(ActivationContext activationContext)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

有人对这个问题有任何想法吗?我已经将[assembly:AllowPartiallyTrustedCallers]添加到程序集中。有没有办法找出哪个引用(?)导致此错误?或者通过InitializeComponent()的任何方式?

注意:我已包含所有权限,项目已设置为部分信任。

无论如何,非常感谢任何帮助。

3 个答案:

答案 0 :(得分:7)

好的,如果我要解决这个问题,我会按如下方式处理:

1)如果我使用的是.NET 4.0,请确保已经处理this

2)使用ILDASM或反射器打开bin文件夹中的所有DLL,以确保在它们上设置AllowPartiallyTrustedCallersAttribute

3)在出错时使用AppDomain.CurrentDomain.GetAssemblies()(使用即时窗口)查看从哪里加载哪个程序集。我认为这可能是你的问题,因为我经常看到从GAC或各种bin文件夹加载旧的或流氓版本的程序集

我认为使用这3个步骤,您将能够找到您的问题。

答案 1 :(得分:7)

对于所有未来的读者谁可能错过了Aliostad的答案下的评论。

基本上对我有用的是 Aliostad的建议并重新编译我可以使用 AllowPartiallyTrustedCallersAttribute 的所有引用。要验证加载的程序集我遵循第一步 Aliostad的建议。一旦我确保所有必需的dll都具有该属性,我将该属性包含在我的项目中,然后将我的项目设置为完全信任(不是部分信任)。

注意:我正在使用Microsoft.Office.Interop.Outlook发送电子邮件,它需要完全信任,但仍允许其他dll以部分信任方式运行。

希望这有助于未来的用户。任何问题只需在下面评论。

答案 2 :(得分:3)

从网络位置运行dll时出现此错误。 dll是使用ArcGIS 10.1中运行的arcobjects扩展ESRI ArcGIS。解决方案不能从网络位置打开项目。