I use LINQPad to check the code (which should be a great product, I have to say), but now I encounter an exception when I try to set Thread.CurrentPrincipal to a custom IPrincipal that is marked with a SerializableAttribute following a sample demonstrating the problem
void Main() { Thread.CurrentPrincipal = new MyCustomPrincipal(); } // Define other methods and classes here [Serializable] public class MyCustomPrincipal : IPrincipal { public bool IsInRole(string role) { return true; } public IIdentity Identity { get { return new WindowsIdentity("RECUPERA\\m.casamento"); } } }
When I run this code in LINQPad (C # Program as Language), I get the following exception
Type is not resolved for member 'UserQuery+MyCustomPrincipal,query_nhxfev, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' RuntimeMethodInfo: PluginWindowManager.get_Form ()
If I remove the Serializable attribute, everything will be fine. The problem seems to be related to the AppDomain architecture that LINQPad uses, and the inability of the structure to find the assembly that defines MyCustomPrincipal. In addition, I believe that defining MyCustomPrincipal in another assembly and including it in the GAC will solve the problem, but this is not an option for me. Does anyone have an idea?
Thanks Marco
EDIT : I donβt know if this can help, but I had the same problem with SqlDependency.Start: setting Serializable to IPrincipal caused the framework to throw an error complaining that it might not find the assembly that defines the IPrincipal type. I decided with a shameful hack:
System.Security.Principal.IPrincipal principal; principal = System.Threading.Thread.CurrentPrincipal; System.Threading.Thread.CurrentPrincipal = null; try { SqlDependency.Start(connectionString); m_SqlDependencyStarted = true; } catch (Exception ex) { throw (ex); } finally { System.Threading.Thread.CurrentPrincipal = principal; }
source share