WF4.0 基础篇 (二十九) WorkflowInspectionServices

 

本文例子下载:

http://files.cnblogs.com/foundation/WorkflowInspectionServicesSample.rar

 

WorkflowInspectionServices 类

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,所以流程图中的

  • FlowNode
  • FlowStep
  • FlowSwitch
  • FlowDecision

不是从Activity中继承的,所以WorkflowInspectionServices无法得到这些元素,但这些元素中所包含的Activity可以得到

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第1张图片

 

 

静态方法 GetActivities

基本功能

基于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);

}

}

 

 

}

 

例子1:Sequence嵌套

static void showTestAWorkflow()

{

Activity wf = getActivity("testAWorkflow.xaml");

printActivityTree(wf, "");

 

System.Console.WriteLine("=======================");

printActivityStruce(wf, "");

}

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第2张图片

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第3张图片

 

例子2:IF容器

static void showTestBWorkflow()

{

Activity wf = getActivity("testBWorkflow.xaml");

printActivityTree(wf, "");

 

System.Console.WriteLine("=======================");

printActivityStruce(wf, "");

 

 

}

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第4张图片

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第5张图片

 

例子3:FlowChar

static void showTestCWorkflow()

{

Activity wf = getActivity("testCWorkflow.xaml");

printActivityTree(wf, "");

 

System.Console.WriteLine("=======================");

printActivityStruce(wf, "");

}

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第6张图片

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第7张图片

 

例子4:自定义Activity

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

}

}

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第8张图片

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第9张图片

 

 

 

 

操作WCF服务中的工作流

xamlx文件中的工作流,可以用XamlServices.Load得到WorkflowService,WorkflowService的Body就对应WCF服务中的Activity树

FileStream fs = new FileStream("Service1.xamlx", FileMode.Open);

WorkflowService wfs = (WorkflowService)XamlServices.Load(fs);

 

printActivityTree(wf,"|--");

 

 

 

静态方法 Resolve

返回流程树中指定id的Activity

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第10张图片

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

}

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第11张图片

 

 

 

静态方法 CacheMetadata

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

}

}

 

WF4.0 基础篇 (二十九) WorkflowInspectionServices_第12张图片

 

 

你可能感兴趣的:(workflow)