ctescu
ctescu

Reputation: 370

.Net Thread.CurrentPrincipal

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);
}

Upvotes: 1

Views: 1132

Answers (1)

Pete Stensønes
Pete Stensønes

Reputation: 5685

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.

Upvotes: 4

Related Questions