共用方式為


工作流程執行屬性

透過執行緒區域存儲(TLS),CLR 會維護每個執行緒的執行內容。 此執行上下文會控管已知的執行緒屬性,例如執行緒身份、背景交易以及目前的權限集,以及使用者定義的執行緒屬性,例如具名插槽。

不同於直接以 CLR 為目標的程式,工作流程程式是階層式範圍的活動樹狀結構,這些樹狀結構會在線程無關的環境中執行。 這表示標準 TLS 機制無法直接用來判斷指定工作專案的範圍內容。 例如,兩個平行執行分支可能會使用不同的交易,但排程器可能會在相同的 CLR 線程上交錯執行。

工作流程執行屬性提供一種機制,將內容特定屬性新增至活動的環境。 這可讓活動宣告其子樹範圍中的屬性,並提供設定和卸除 TLS 的勾點,以適當地與 CLR 物件互作。

建立和使用工作流程執行屬性

工作流程執行屬性通常會實作IExecutionProperty介面,不過著重於傳訊的屬性可能會實作ISendMessageCallbackIReceiveMessageCallback。 若要建立工作流程執行屬性,請建立一個實作 IExecutionProperty 介面的類別,並實作其成員 SetupWorkflowThreadCleanupWorkflowThread。 這些成員為執行屬性提供機會,可以在每次包含該屬性的活動及其子活動的工作週期中,正確設置及釋放線程本地儲存空間。 在此範例中,ConsoleColorProperty 會建立以設定 Console.ForegroundColor

class ConsoleColorProperty : IExecutionProperty  
{  
    public const string Name = "ConsoleColorProperty";  
  
    ConsoleColor original;  
    ConsoleColor color;  
  
    public ConsoleColorProperty(ConsoleColor color)  
    {  
        this.color = color;  
    }  
  
    void IExecutionProperty.SetupWorkflowThread()  
    {  
        original = Console.ForegroundColor;  
        Console.ForegroundColor = color;  
    }  
  
    void IExecutionProperty.CleanupWorkflowThread()  
    {  
        Console.ForegroundColor = original;  
    }  
}  

活動作者可以在活動的執行覆寫中註冊這個屬性。 在這個範例中,一個ConsoleColorScope活動被定義,會將ConsoleColorProperty新增到目前PropertiesNativeActivityContext集合中註冊。

public sealed class ConsoleColorScope : NativeActivity  
{  
    public ConsoleColorScope()  
        : base()  
    {  
    }  
  
    public ConsoleColor Color { get; set; }  
    public Activity Body { get; set; }  
  
    protected override void Execute(NativeActivityContext context)  
    {  
        context.Properties.Add(ConsoleColorProperty.Name, new ConsoleColorProperty(this.Color));  
  
        if (this.Body != null)  
        {  
            context.ScheduleActivity(this.Body);  
        }  
    }  
}  

當活動主體開始工作脈衝時,會呼叫屬性的SetupWorkflowThread方法,而當工作脈衝完成時,會呼叫CleanupWorkflowThread。 在此範例中,會建立一個使用具有三個 Parallel 分支的活動的工作流程。 前兩個分支會使用 ConsoleColorScope 活動,而第三個分支則不使用。 這三個分支都包含兩 WriteLine 個活動和一個 Delay 活動。 Parallel活動執行時,分支中包含的活動會以交錯方式執行,但在每個子活動執行時,由ConsoleColorProperty套用正確的控制台色彩。

Activity wf = new Parallel  
{  
    Branches =
    {  
        new ConsoleColorScope  
        {  
            Color = ConsoleColor.Blue,  
            Body = new Sequence  
            {  
                Activities =
                {  
                    new WriteLine  
                    {  
                        Text = "Start blue text."  
                    },  
                    new Delay  
                    {  
                        Duration = TimeSpan.FromSeconds(1)  
                    },  
                    new WriteLine  
                    {  
                        Text = "End blue text."  
                    }  
                }  
            }  
        },  
        new ConsoleColorScope  
        {  
            Color = ConsoleColor.Red,  
            Body = new Sequence  
            {  
                Activities =
                {  
                    new WriteLine  
                    {  
                        Text = "Start red text."  
                    },  
                    new Delay  
                    {  
                        Duration = TimeSpan.FromSeconds(1)  
                    },  
                    new WriteLine  
                    {  
                        Text = "End red text."  
                    }  
                }  
            }  
        },  
        new Sequence  
        {  
            Activities =
            {  
                new WriteLine  
                {  
                    Text = "Start default text."  
                },  
                new Delay  
                {  
                    Duration = TimeSpan.FromSeconds(1)  
                },  
                new WriteLine  
                {  
                    Text = "End default text."  
                }  
            }  
        }  
    }  
};  
  
WorkflowInvoker.Invoke(wf);  

叫用工作流程時,會將下列輸出寫入主控台視窗。

Start blue text.  
Start red text.  
Start default text.  
End blue text.  
End red text.  
End default text.  

備註

雖然它未顯示在上一個輸出中,但控制台視窗中的每一行文字都會以指定的色彩顯示。

工作流程執行屬性可由自定義活動作者使用,而且它們也會提供機制來管理活動,例如 CorrelationScopeTransactionScope 活動。

另請參閱