场景:我正在重构我们的一个应用程序以使用 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() 的任何方式?
注意:我拥有所有权限,并且项目设置为部分信任。
无论如何,我们将不胜感激。
最佳答案
好的,如果我要解决这个问题,我会按以下方式处理:
1) 如果我使用的是 .NET 4.0,请确保 this已经处理过了。
2) 使用 ILDASM 或反射器打开 bin 文件夹中所有有问题的 DLL,以确保在它们上设置了 AllowPartiallyTrustedCallersAttribute
。
3) 在出错时(使用即时窗口)使用 AppDomain.CurrentDomain.GetAssemblies()
查看从哪里加载了哪个程序集。我认为这可能是您的问题,因为我经常看到旧版本或流氓版本的程序集是从 GAC 或各种 bin 文件夹加载的
我认为使用这 3 个步骤您将能够找到您的问题。
关于c# - 该程序集不允许部分受信任的调用方。初始化组件(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4314420/