如何获取 WorkflowApplication 的上下文? [英] HOW to get Context of WorkflowApplication?

查看:28
本文介绍了如何获取 WorkflowApplication 的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个类似于 Visual Workflow Tracking* 的工作流设计器.

I'm making a Workflow designer similar to Visual Workflow Tracking*.

我想添加一个新控件来调试 wf,但我不知道如何访问 wf 上下文

I would like add a new control to debug wf, but id don't know how to access to wf context

要运行 WF 我使用这个:

To Run WF I Use this :

 WFApplication wfApp = new WorkflowApplication(act, inputs);

我的想法是当我收到跟踪事件时,获取 wfApp 的上下文,以获取变量或参数值.

My idea is when i recive trace event, get context of wfApp, to get vars or arguments values.

有可能吗?

*您可以从以下位置下载 VisualStudioTracking 代码:适用于 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例进而:\WF_WCF_Samples\WF\Application\VisualWorkflowTracking*

*You can donwload VisualStudioTracking Code From : Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 and then :\WF_WCF_Samples\WF\Application\VisualWorkflowTracking*

推荐答案

我终于解决了这个问题.

Finally I solved the problem.

首先,我从 xaml 中获取所有参数名称和工作流变量.

First I get all arguments names, and variables of workflow, from xaml.

    class XamlHelper
    {   

    private string xaml;

    public XamlHelper(string xaml)
    {           
        this.xaml = xaml;

        DynamicActivity act = GetRuntimeExecutionRoot(this.xaml);
        ArgumentNames = GetArgumentsNames(act);

     GetVariables(act);
    }

    private void GetVariables(DynamicActivity act)
    {
        Variables = new List<string>();
        InspectActivity(act);


    }

    private void InspectActivity(Activity root)
    {


        IEnumerator<Activity> activities =
            WorkflowInspectionServices.GetActivities(root).GetEnumerator();


        while (activities.MoveNext())
        {

            PropertyInfo propVars = activities.Current.GetType().GetProperties().FirstOrDefault(p => p.Name == "Variables" && p.PropertyType == typeof(Collection<Variable>));
            if (propVars != null)
            {
                try
                {
                    Collection<Variable> variables = (Collection<Variable>)propVars.GetValue(activities.Current, null);
                    variables.ToList().ForEach(v =>
                    {
                        Variables.Add(v.Name);

                    });
                }
                catch
                {

                }
            }
            InspectActivity(activities.Current);
        }
    }





    public List<string> Variables
    {
        get;
        private set;
    }

    public List<string> ArgumentNames
    {
        get;
        private set;
    }


    private DynamicActivity GetRuntimeExecutionRoot(string xaml)
    {


        Activity root = ActivityXamlServices.Load(new StringReader(xaml));


            WorkflowInspectionServices.CacheMetadata(root);

        return root as DynamicActivity;

    }

    private List<string> GetArgumentsNames(DynamicActivity act)
    {
        List<string> names = new List<string>();
        if (act != null)
        {
            act.Properties.Where(p => typeof(Argument).IsAssignableFrom(p.Type)).ToList().ForEach(dp =>
            {
                names.Add(dp.Name);
            });

        }

        return names;
    }

}

然后我用这些参数和变量名创建跟踪

Second I create trace with these arguments and variable names

    private static WFTrace CreateTrace(List<string> argumentNames, List<string> variablesNames)
    {
        WFTrace trace = new WFTrace();
        trace.TrackingProfile = new TrackingProfile()
        {
            ImplementationVisibility = ImplementationVisibility.All,
            Name = "CustomTrackingProfile",
            Queries = 
                {
                    new CustomTrackingQuery() 
                    {
                     Name = all,
                     ActivityName = all
                    },
                    new WorkflowInstanceQuery()
                    {


                         // Limit workflow instance tracking records for started and completed workflow states
                        States = {WorkflowInstanceStates.Started, WorkflowInstanceStates.Completed },
                   }
                }
        };

        trace.TrackingProfile.Queries.Add(GetActivityQueryState(argumentNames, variablesNames));

        return trace;
    }

然后调用 wf 并添加 traceextension.

And then invoke wf and add traceextension.

Adam 这是代码

  private TrackingQuery GetActivityQueryState(List<string> argumentNames, List<string> variablesNames)
        {
            ActivityStateQuery query = new ActivityStateQuery()
            {
                ActivityName = "*",
                States = { ActivityStates.Executing, ActivityStates.Closed }
            };
            if (argumentNames != null)
            {
                argumentNames.Distinct().ToList().ForEach(arg =>
                {
                    query.Arguments.Add(arg);
                });
            }
            if (variablesNames != null)
            {
                variablesNames.Distinct().ToList().ForEach(v =>
                {
                    query.Variables.Add(v);
                });
            }
            return query;
        }

这篇关于如何获取 WorkflowApplication 的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆