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可以得到
靜態方法 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, "");
}
例子2:IF容器
static void showTestBWorkflow()
{
Activity wf = getActivity("testBWorkflow.xaml");
printActivityTree(wf, "");
System.Console.WriteLine("=======================");
printActivityStruce(wf, "");
}
例子3:FlowChar
static void showTestCWorkflow()
{
Activity wf = getActivity("testCWorkflow.xaml");
printActivityTree(wf, "");
System.Console.WriteLine("=======================");
printActivityStruce(wf, "");
}
例子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" };
}
}
操作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
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
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);
}
}
本文配套源碼:http://www.bianceng.net/dotnet/201208/261.htm