1

In what condition the principal is lost for the current thread. I have an Windows Form application that uses principal for main thread and receive notification via WCF from a server. On some clients I loose the Principal for the current thread and I don't understand why. The "lost" seems to be from the code:

foreach (EventHandler subscriber in onApplicationIdle.GetInvocationList())
{
    subscriber.BeginInvoke(this, e, OnAsyncCompleted, subscriber);
}
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
ctescu
  • 350
  • 4
  • 16
  • can you post some code, and at which point `CurrentPrincipal` "disappears"? – Yura Apr 20 '15 at 13:14
  • I am trying to understand why is lost. The code where I see is foreach (EventHandler subscriber in onApplicationIdle.GetInvocationList()) { subscriber.BeginInvoke(this, e, OnAsyncCompleted, subscriber); } – ctescu Apr 20 '15 at 13:18
  • 3
    I mean add more info about your application flows. Something like "Calling WCF Service -> Receive async response-> UpdateUI (here `CurrentPrincipal` is null)". Just post some wider scope of your code. – Yura Apr 20 '15 at 13:22

1 Answers1

4

When you create a new thread in .NET the principal of the parent thread is not automatically propagated. You must do that yourself if you are making the thread.

If you are using async I believe it has its own rules for identity propagation in any threads it creates.

Calling BeginInvoke puts the method in the ThreadPool (I believe!) so thread pool principals apply here. I think that means you have to do it yourself!

You can have all threads YOU make set the principal automatically by invoking AppDomain.SetPrincipalPolicy, but this only covers the three types in the PrincipalPolicy enum.

Otherwise its up to you in the threaded code to set the Thread.Current.Principal() by hand.

Please see the wrong-thread-currentprincipal-in-async-wcf-end-method Stack overflow post for a similar discussion.

Community
  • 1
  • 1
Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62