I\'m having a problem invoking WCF services in parallel. I\'m trying to use Tasks to invoke two services in parallel to save some time (I\'m NOT trying to make this async),
Try this:
WindowsIdentity impersonatedUser = WindowsIdentity.GetCurrent();
Task<object>.Factory.StartNew(() =>
{
using (WindowsImpersonationContext ctx = impersonatedUser.Impersonate())
{
//Your code
}
return data;
});
You might want to look into whether setting <alwaysFlowImpersonationContext> is appropriate:
Specifies that the Windows identity always flows across asynchronous points, regardless of how impersonation was performed.
By default, this setting is off within ASP.Net, and so the tasks (running, potentially, on other threads) end up just running with no impersonation in place.
Otherwise (if it's not appropriate to set that), I think you could capture the WindowsIdentity
object into a local variable that's captured by the Task
s, and then call Impersonate() on it from within the task. (although I haven't tried this)