Linqpad and custom IPrincipal serializable

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; } 
+6
source share
3 answers

One trick you can use is the strong name of the assembly containing the custom core element and puts it in the global assembly cache. Thus, any application can find the assembly you need, this is not an ideal solution, because after that you will have to delete it again. In any case, to install in the GAC, if you have Visual Studio installed, run the VS command prompt and do the following:

gacutil -i nameofassembly.dll

0
source

Blow in the dark, but could it be a signing due to the intersection of assemblies?

0
source

This seems to be a serialization issue in LinqPad. I see that Joseph Albahari, creator of Linqpad, has confirmed this in your comments.

Bottom line: for this we need to fix in LinqPad. I do not know any work.

0
source

Source: https://habr.com/ru/post/920420/


All Articles