2011-04-08 76 views
4

我遇到了Thread.CurrentPrincipal没有从主线程传播到工作流应用程序的问题。Windows工作流和Thread.CurrentPrincipal

有没有办法做到这一点?

这是我的示例应用程序:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new string[] { "managers", "executives" }); 

     System.Console.WriteLine("MainThread Prinicipal type: " + Thread.CurrentPrincipal.GetType().ToString()); 
     System.Console.WriteLine("MainThread Prinicipal Identity: " + Thread.CurrentPrincipal.Identity.Name); 
     System.Console.WriteLine(); 

     AutoResetEvent syncEvent = new AutoResetEvent(false); 

     WorkflowApplication application = new WorkflowApplication(new Workflow1()); 

     application.Completed = delegate(WorkflowApplicationCompletedEventArgs e) 
     { 
      syncEvent.Set(); 
     }; 

     application.Run(); 
     syncEvent.WaitOne(); 
    } 
} 

和工作流

<Activity mc:Ignorable="sap" x:Class="WorkflowConsoleApplication1.Workflow1" sap:VirtualizedContainerService.HintSize="273,427" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Threading;assembly=System.Core" xmlns:st1="clr-namespace:System.Threading;assembly=System" xmlns:st2="clr-namespace:System.Text;assembly=mscorlib" xmlns:st3="clr-namespace:System.Threading;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Sequence sad:XamlDebuggerXmlReader.FileName="C:\projects\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml" sap:VirtualizedContainerService.HintSize="233,387"> 
     <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="[&quot;WorkflowThread Prinicipal type: &quot; + System.Threading.Thread.CurrentPrincipal.GetType().ToString()]" /> 
     <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="[&quot;WorkflowThread Prinicipal Identity: &quot; + System.Threading.Thread.CurrentPrincipal.Identity.Name]" /> 
     <WriteLine sap:VirtualizedContainerService.HintSize="211,61" /> 
    </Sequence> 
</Activity> 

从上面的程序的输出是:

MainThread类型PRINICIPAL:System.Security.Principal .GenericPrincipal
MainThread主要身份:Bob

WorkflowThread类型PRINICIPAL:System.Security.Principal.GenericPrincipal
WorkflowThread身份PRINICIPAL:

任何帮助将不胜感激。

谢谢

回答

3

WorkflowApplication使用其SynchronizationContext安排实际工作。默认实现使用ThreadPool as user7116 described。然而,您可以自由实施您自己的SynchronizationContext并实施Post()操作来执行任何您想要的操作。

+0

Damnit,你还在观看[workflow-foundation-4] rss feed吗? – Will 2011-04-08 17:31:35

+0

是的:-)现在为什么评论必须有一个最小长度? – Maurice 2011-04-08 17:35:58

+0

它的烦人,但如果你得到[这个用户脚本](http://stackapps.com/questions/2051/reply-links-on-comments)它可以更容易地有最小长度的评论(插入@username:在评论开头)(编辑:@maurice固定链接) – Will 2011-04-08 17:41:40

3

我相信,既然Workflow S于该ThreadPool操作也不会继承你的主线程上设置任何本金。您可以尝试changing the principal of the AppDomain

// per MSDN: Create a principal to use for new threads. 
IPrincipal principal = new GenericPrincipal(
    new GenericIdentity("Bob", "Passport"), 
    new string[] { "managers", "executives" }); 
AppDomain.CurrentDomain.SetThreadPrincipal(principal);