gpt4 book ai didi

workflow-foundation-4 - 如何获取 WorkflowApplication 的上下文?

转载 作者:行者123 更新时间:2023-12-02 22:36:21 24 4
gpt4 key购买 nike

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

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

要运行 WF,我使用这个:

 WFApplication wfApp = new WorkflowApplication(act, inputs);

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

有可能吗?

*您可以从以下位置下载 VisualStudioTracking 代码: Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4进而:\WF_WCF_Samples\WF\Application\VisualWorkflowTracking*

最佳答案

最后我解决了这个问题。

首先,我从 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;
}

}

其次,我使用这些参数和变量名创建跟踪

    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。

亚当,这是代码

  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;
}

关于workflow-foundation-4 - 如何获取 WorkflowApplication 的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com