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

2023-09-07 02:30:31 作者:你非良人i

方案:我在重构我们使用NHibernate的应用程序之一的过程中,过这个问题来了几个星期前。这个问题本来是与NHibernate和城堡,解决这一点,他们都是重新编译与 [总成:AllowPartiallyTrustedCallers] 。但是之后做了一些更改UI和codeBase的这个错误再次重新出现。另外值得一提的是,我控制编程方式从Form_Main装载我的用户控件。

Scenario: I am in the process of refactoring one of our applications to use Nhibernate and came across this issue a couple weeks back. The issue was originally with Nhibernate and Castle and to solve this they were both recompiled with the [assembly: AllowPartiallyTrustedCallers]. However after making some changes to the UI and codebase this error has reappeared again. Also worth noting is that I control the loading my user controls programmatically from Form_Main.

问题:无论何时生成的用户控件我收到以下错误。如果我注释掉加载程序将运行。当我调试它在在InitializeComponent()函数,它是自动生成的结束。请注意,我不能踏入的功能。

Problem: Whenever the user controls are generated I receive the error below. If I comment out the loading then the program will run. When I debug it ends at the InitializeComponent() function which is auto-generated. Note that I can't step into that function.

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: 

任何人有关于这个问题的任何想法?我已经添加[总成:AllowPartiallyTrustedCallers]大会。有没有办法找出哪些引用(?)是导致此错误?或者有什么办法来逐步的InitializeComponent()?

Anyone have any ideas on the subject? I have already added [assembly: AllowPartiallyTrustedCallers] to the assembly. Is there any way to find out which reference(?) is causing this error? Or any way to step through InitializeComponent()?

注意:我有充分的权限包括和项目设置为部分信任

NOTE: I have every permission included and the project is set to partial trust.

反正任何帮助是极大的AP preciated。

Anyways any help is greatly appreciated.

推荐答案

确定,如果我要解决这个问题,我想接近它,如下:

OK, if I were to troubleshoot this issue, I would approach it as below:

1)如果我使用.NET 4.0确保这个已处理的。

1) If I am using .NET 4.0 make sure this is already handled.

2)使用ILDASM或反射器打开有问题的所有DLL的bin文件夹,以确保 AllowPartiallyTrustedCallersAttribute程序设置在他们身上。

2) Use ILDASM or reflector to open all DLLs in question on the bin folder to make sure AllowPartiallyTrustedCallersAttribute is set on them.

3)使用 AppDomain.CurrentDomain.GetAssemblies()在错误的时间(使用立即窗口),看看哪个组件从那里加载。这个我想可能是你的问题,因为我见过太多往往是旧的或流氓版本的组件都是由海关总署或各种箱文件夹加载

3) Use AppDomain.CurrentDomain.GetAssemblies() at the time of error (using immediate window) to see which assembly is loaded from where. This I think could be your problem as I have seen too often that old or rogue versions of assemblies are loaded from GAC or various bin folders

我觉得用这3个步骤,你就能找到你的问题。

I think using these 3 steps you will be able to find your problem.