WCF并行模拟 [英] WCF Parallel Impersonation
问题描述
我有ImpersonationOption.RequiredWCF服务。该模拟似乎不使用并行时,流过。例如:
I have a WCF service with "ImpersonationOption.Required". The impersonation does not seem to flow through when using parallelism. For example:
Parallel.ForEach(items => results.Add(SystemUtil.WindowsUser.Name)
将返回模拟的用户号码,并用应用程序池的用户号码。可以模拟进行并行性工作?
Will return a number with the impersonated user, and a number with the app pool user. Can impersonation be made to work with parallelism?
最好的,
马克
< STRONG>更新:
这是在IIS服务端实际的代码
This is actual code on the IIS Service side.
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string[] WhoAmI(int numOfTests)
{
var tests = new List<int>();
for (var i = 0; i < numOfTests; i++)
tests.Add(i);
var results = new ConcurrentBag<string>();
Parallel.ForEach(tests, (test) => results.Add(WindowsIdentity.GetCurrent(false).Name));
return results.ToArray();
}
如果我通过在numOfTests = 10,它产生10个任务,并把每个任务的WindowsIndentity名称。我得到的是〜70%IIS APPPOOL.NET V4.0,并〜30%我。
If I pass in numOfTests = 10, it spawns 10 tasks and return the WindowsIndentity Name of each task. What I get is ~70% "IIS APPPOOL.NET v4.0" and ~30% me.
我如何设置,这样我的身份总是让入Parallel.ForEach?
How can I set it such that my identity always makes it into the Parallel.ForEach?
谢谢!
推荐答案
您需要采取照顾自己。尝试是这样的:
You need to take care of that yourself. Try something like this:
IntPtr token = WindowsIdentity.GetCurrent().Token;
Parallel.ForEach( Enumerable.Range( 1, 100 ), ( test ) =>
{
using ( WindowsIdentity.Impersonate( token ) )
{
Console.WriteLine( WindowsIdentity.GetCurrent( false ).Name );
}
} );
这篇关于WCF并行模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!