本文例子下载:
http://files.cnblogs.com/foundation/WorkflowInspectionServicesSample.rar
WorkflowInspectionServices可以得到流程中的Activity,
由于WF4.0的ActivityTree相对复杂,并不是象WF3.X的结构那样清晰,
在WF4.0中Activitys,Body,Implementation等了Activity属性比效多,要循环Activity就要做很多判断,这时可以使用WorkflowInspectionServices得到ActivityTree上的所有Activity.
WorkflowInspectionServices可自动判断Activitys,Body,Implementation等
与自已实现递归相比,自已实现递归可以操作更多细节,WorkflowInspectionServices则可以快速得到全部Activity
注意:由于WorkflowInspectionServices只得到Activity,所以流程图中的
不是从Activity中继承的,所以WorkflowInspectionServices无法得到这些元素,但这些元素中所包含的Activity可以得到
基于XAML文件名得到Activity |
static Activity getActivity(string filePathName) {
string valueString = "";
using (System.IO.FileStream fileStream = new System.IO.FileStream(filePathName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { using (System.IO.StreamReader streamReader = new System.IO.StreamReader(fileStream)) { valueString = streamReader.ReadToEnd(); } }
System.IO.StringReader stringReader = new System.IO.StringReader(valueString);
Activity activity = System.Activities.XamlIntegration.ActivityXamlServices.Load(stringReader);
WorkflowInspectionServices.CacheMetadata(activity);
return activity;
} |
|
GetActivities方式 |
static void printActivityTree(Activity activity,string tag) {
Console.WriteLine(tag + "{0},type:{1}", activity.DisplayName, activity.GetType());
System.Collections.Generic.IEnumerator<Activity> list = WorkflowInspectionServices.GetActivities(activity).GetEnumerator();
while (list.MoveNext()) { printActivityTree(list.Current," "+ tag ); } } |
|
递归方式 |
static void printActivityStruce(Activity activity, string tag) {
System.Console.WriteLine(tag + "{0},Type:{1}", activity.DisplayName, activity.GetType());
//如果是表达式 VisualBasicValue<string> a = activity as VisualBasicValue<string>;
if (a != null) { System.Console.WriteLine(tag + a.ExpressionText); }
//如果是[DynamicActivity] DynamicActivity dynamicActivity = activity as DynamicActivity; if (dynamicActivity != null) { System.Console.WriteLine(tag + " Properties:"); foreach (var item in dynamicActivity.Properties) { System.Console.WriteLine(tag + " Name:{0}, Type:{1}", item.Name, item.Type); }
System.Console.WriteLine(tag + " Implementation:");
tag = tag + " "; printActivityStruce(dynamicActivity.Implementation(), tag); }
//如果是[Sequence] Sequence sequence = activity as Sequence; if (sequence != null) { System.Console.WriteLine(tag + " Variables :"); foreach (var item in sequence.Variables) { System.Console.WriteLine(tag + " Name:{0}, Type:{1}, Default:{2}", item.Name, item.Type, item.Default); }
System.Console.WriteLine(tag + " Activities:"); foreach (var item in sequence.Activities) { tag = tag + " "; printActivityStruce(item, tag); } }
} |
static void showTestAWorkflow() { Activity wf = getActivity("testAWorkflow.xaml"); printActivityTree(wf, "");
System.Console.WriteLine("======================="); printActivityStruce(wf, ""); } |
static void showTestBWorkflow() { Activity wf = getActivity("testBWorkflow.xaml"); printActivityTree(wf, "");
System.Console.WriteLine("======================="); printActivityStruce(wf, "");
} |
static void showTestCWorkflow() { Activity wf = getActivity("testCWorkflow.xaml"); printActivityTree(wf, "");
System.Console.WriteLine("======================="); printActivityStruce(wf, ""); } |
static void showTestDWorkflow() { Activity wf = getActivity("testDWorkflow.xaml"); printActivityTree(wf, "");
System.Console.WriteLine("======================="); printActivityStruce(wf, ""); } |
public sealed class muActivity : Activity { protected override Func<Activity> Implementation { get { return getActivity; } set { base.Implementation = value; } }
Activity getActivity() { return new WriteLine { DisplayName = "wxwinter" }; } } |
xamlx文件中的工作流,可以用XamlServices.Load得到WorkflowService,WorkflowService的Body就对应WCF服务中的Activity树
FileStream fs = new FileStream("Service1.xamlx", FileMode.Open); WorkflowService wfs = (WorkflowService)XamlServices.Load(fs);
printActivityTree(wf,"|--"); |
返回流程树中指定id的Activity
static void printActivityID(Activity activity, string tag) {
Console.WriteLine(tag + "{0},{1}", activity.Id, activity.DisplayName);
System.Collections.Generic.IEnumerator<Activity> list = WorkflowInspectionServices.GetActivities(activity).GetEnumerator();
while (list.MoveNext()) { printActivityID(list.Current, " " + tag); } }
static void findActivityByID() { Activity wf = getActivity("testAWorkflow.xaml"); printActivityID(wf, ""); System.Console.WriteLine("=======================");
Activity act= WorkflowInspectionServices.Resolve(wf, "1.5"); Console.WriteLine("{0},{1}", act.Id, act.DisplayName); } |
CacheMetadata 方法用于验证Activity,如果验证不通过会产生异常,所以应该放入TryCatch中
Activity |
public sealed class myActivityA : CodeActivity {
public InArgument<string> myText { get; set; }
protected override void CacheMetadata(CodeActivityMetadata metadata) { myText = new VisualBasicValue<string> { ExpressionText = "myIn" }; base.CacheMetadata(metadata); }
protected override void Execute(CodeActivityContext context) {
string text = context.GetValue(this.myText);
System.Console.WriteLine("myActivityA:" + text); } } |
static void testCacheMetadata() { try { System.Console.WriteLine("验证 myActivityA"); WorkflowInspectionServices.CacheMetadata(new myActivityA()); System.Console.WriteLine("验证 myActivityA 通过"); } catch(System.Exception ex) { System.Console.WriteLine(ex.Message); }
System.Console.WriteLine("/====================================================================");
DynamicActivity dynamicActivity = new DynamicActivity();
dynamicActivity.Properties.Add(new DynamicActivityProperty { Name = "myIn", Type = typeof(InArgument<string>), Value = new InArgument<string>() });
dynamicActivity.Implementation =()=>new myActivityA();
try { System.Console.WriteLine("验证 dynamicActivity"); WorkflowInspectionServices.CacheMetadata(dynamicActivity); System.Console.WriteLine("验证 dynamicActivity 通ª过"); } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } |
|