That assembly does not allow partially trusted callers. InitializeComponent()
Asked Answered
H

3

11

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.

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: 

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.

Anyways any help is greatly appreciated.

Helminthic answered 30/11, 2010 at 13:47 Comment(3)
C:\Documents and Settings\ID\Desktop, is that a real path or did you edit it? Apps that run from the C: drive should always run with full trust. Recompiling only hid the real problem.Hypoplasia
@Hans Passant, ID is my User name which I removed. Right now though I'm running the program with the selected permissions from my local machine so that I don't have to keep publishing to the network share to test each time.Helminthic
I would have to guess that "selected permissions" is your problem. Not documenting this in your question is a big mistake.Hypoplasia
T
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.

Temblor answered 30/11, 2010 at 14:3 Comment(7)
When I try to use AppDomain.GetAssemblies() it gives me this error "An object reference is required for the non-static field, method, or property 'System.AppDomain.GetAssemblies()'" I am using it in the "Immediate Window".Helminthic
Sorry! Use AppDomain.Current.GetAssemblies()Temblor
@Aliostad, was able to run it and it shows that 22 assemblies were loaded. Would that mean that the last one is the one causing the error?Helminthic
Examine each and every assembly: AppDomain.Current.GetAssemblies()[0], AppDomain.Current.GetAssemblies()[1], ..Temblor
@Aliostad, I found the assemblies that were giving me the error and changed them to AllowPartialTrustedCallers. But now I'm getting errors when I try to launch a PDF as well as when I try to create an email(not send it) in Outlook. Any idea on how this can be accomplished? Should I just ask another question?Helminthic
Great that you found the problem. So was it a rogue DLL, wasn't it. In order not to pollute the question, please create a new one and post the address here.Temblor
@Aliostad, I ended up figuring it out anyways. Basically I set my application to full trust but still kept the AllowPartialTrustedCallers in my AssemblyInfo and everything works now. Thanks again.Helminthic
H
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.

Helminthic answered 6/12, 2010 at 13:35 Comment(0)
C
3

I got this error when running a dll from a network location. The dll was an extension for ESRI ArcGIS using arcobjects running in ArcGIS 10.1. Solution don't open project from network location.

Che answered 2/4, 2014 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.