简体   繁体   English

Windows Workflow中的拦截/装饰活动

[英]Intercepting/Decorating Activities in Windows Workflow

Does Windows Workflow Foundation offer a way to intercept or decorate activities for purposes such as logging? Windows Workflow Foundation是否提供了一种拦截或装饰活动的方法,例如记录? For example to create logs for every activity entry and exit point (ideally including the activity name) without modifying all the existing project code. 例如,为每个活动入口和出口点创建日志(理想情况下包括活动名称),而不修改所有现有项目代码。

For example, I have a workflow with a single activity that prints "Hello world". 例如,我有一个工作流程,其中包含一个打印“Hello world”的活动。 Without making modification to the XAML file I would like to capture the entry and exit of the activity. 在不修改XAML文件的情况下,我想捕获活动的进入和退出。 I would like to print "Entering Hello World Activity" before entering the activity and after the activity has printed "Hello World", I would like to print "Completed Hello World Activity". 我想在进入活动之前打印“输入Hello World活动”,在活动打印出“Hello World”之后,我想打印“已完成的Hello World活动”。

Does Windows Workflow offer a mechanism for hooking into entry and exit of an activity? Windows Workflow是否提供了挂钩进入和退出活动的机制?


@Richard210363 has already added to the comments that this feature is supported by Windows Workflow Foundation - please can the users who chose to close this question consider reversing their decision as the question clearly has a very specific answer using the framework in question? @ Richard210363已经在评论中添加了Windows Workflow Foundation支持此功能 - 请选择关闭此问题的用户是否可以考虑撤销他们的决定,因为问题显然使用相关框架有一个非常具体的答案?

I wrote the following code which is able to track all the workflows and the activities. 我编写了以下代码,可以跟踪所有工作流程和活动。

public class ActivityTracker : TrackingParticipant
{
    protected override void Track(TrackingRecord record, TimeSpan timeout)
    {            
        if (record != null)
        {
            if (record is WorkflowInstanceRecord)
            {
                WorkflowInstanceRecord instanceRecord = record as WorkflowInstanceRecord;
                Console.WriteLine("Workflow Record: Instance: {0} - State: {1} - Definition Identity: {2}", instanceRecord.ActivityDefinitionId, instanceRecord.State, instanceRecord.WorkflowDefinitionIdentity);
            }
            else if (record is ActivityStateRecord)
            {
                ActivityStateRecord instanceRecord = record as ActivityStateRecord;
                Console.WriteLine("Activity Record: Name: {0} - State: {1}", instanceRecord.Activity.Name, instanceRecord.State);
            }
        }            
    }
}

Have a look at the workflow TrackingParticipant class. 看一下工作流TrackingParticipant类。

It acts across all Activities in the workflow similar to AOP in scope. 它作用于工作流中的所有活动,类似于范围内的AOP。

It emits information about the entry and exit of activities. 它会发出有关活动进入和退出的信息。

Create a class that inherits from TrackingParticipant and override the Track method: 创建一个继承自TrackingParticipant的类并覆盖Track方法:

protected override void Track(TrackingRecord record, TimeSpan timeout)
    {
        ActivityStateRecord activityStateRecord = record as ActivityStateRecord;
        string CurrentActivityName = activityStateRecord.Activity.Name,
    }

Then attach your tracking class to the workflow before it runs. 然后在运行之前将跟踪类附加到工作流程。

_workflowApplication.Extensions.Add(_yourWorkFlowTrackingClass);
_workflowApplication.Run();

You can also cast a TrackingRecord to a WorkflowInstanceRecord. 您还可以将TrackingRecord转换为WorkflowInstanceRecord。 Between them, ActivityStateRecord and WorkflowInstanceRecord supply a lot of info about the workflow and its activities. 在它们之间,ActivityStateRecord和WorkflowInstanceRecord提供了有关工作流及其活动的大量信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM