11

シナリオ:私はアプリケーションの 1 つを 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: 

誰でもこの件について何か考えがありますか?[アセンブリ: AllowPartiallyTrustedCallers] を既にアセンブリに追加しています。このエラーの原因となっている参照(?)を特定する方法はありますか? または、InitializeComponent() をステップ実行する方法はありますか?

注:すべての権限が含まれており、プロジェクトは部分信頼に設定されています。

とにかくどんな助けでも大歓迎です。

4

3 に答える 3

7

For all future readers who may have missed the comments under Aliostad's answer.

Basically what worked for me was taking Aliostad's advice and recompiling all the references I could with AllowPartiallyTrustedCallersAttribute. To verify the assemblies that loaded I followed Step 2 of Aliostad's advice. Once I had made sure all the required dll's had that attribute I included that attribute into my project and then set my project to full trust(not partial trust).

Note: I am using Microsoft.Office.Interop.Outlook to send emails and it requires full trust but still allows the other dll's to run in partial trust.

Hope this help future users. Any questions just comment below.

于 2010-12-06T13:35:07.080 に答える
7

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

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

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

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

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

于 2010-11-30T14:03:43.933 に答える