簡體   English   中英

Windows Workflow和Thread.CurrentPrincipal

[英]Windows Workflow and Thread.CurrentPrincipal

我遇到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主要標識:

任何幫助將不勝感激。

謝謝

我相信,由於WorkflowThreadPool上運行,因此它們不會繼承您在主線程上設置的任何主體。 您可以嘗試更改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);

WorkflowApplication使用其SynchronizationContext來安排實際工作。 默認實現使用ThreadPool 作為user7116描述 但是,您可以自由地實現自己的SynchronizationContext並實現Post()操作來執行您想要的任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM