2011-04-08 5 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

젠장, [workflow-foundation-4] rss 피드도보고 있습니까? – Will

+0

Yup :-) 이제 코멘트의 길이가 최소가되어야하는 이유는 무엇입니까? – Maurice

+0

그것의 성가신,하지만 만약 당신이 [이 사용자 스크립트 (http://stackapps.com/questions/2051/reply-links-on-comments) 그것을 얻을 수 있도록 쉽게 길이가 짧은 주석 (삽입 @ 사용자 이름 :에서 코멘트의 시작) (편집 : @maurice fixed link) – Will

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); 
관련 문제