本节主要介绍WF4 中 WorkflowInvoker的使用
本文例子下载:
http://files.cnblogs.com/foundation/WorkflowInvokerSample.rar
本文例子说明
类名 |
System.Activitys.WorkflowInvoker |
文件 |
System.Activities.dll |
结构说明 |
继承 Object 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] |
功能说明 |
WorkflowApplication与WorkflowInvoker在调用流程上的区别 在WF4.0中,可以使用WorkflowApplication与WorkflowInvoker创建并启动实例. 但两者在宿主中的线程方式是不一样的
使用WorkflowInvoker启动工作流时,工作流将附加到宿主的线程中执行 使用WorkflowApplication启动工作流,工作流实例将在宿主线程之外的另一个线程中运行,使用的是线程池方式
具体见[Delay 与WF4中的线程] |
public static TResult Invoke<TResult>(Activity<TResult> workflow);
public static IDictionary<string, object> Invoke(Activity workflow);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs);
public static IDictionary<string, object> Invoke(Activity workflow, IDictionary<string, object> inputs);
public static IDictionary<string, object> Invoke(Activity workflow, TimeSpan timeout);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs, TimeSpan timeout);
public static IDictionary<string, object> Invoke(Activity workflow, IDictionary<string, object> inputs, TimeSpan timeout);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs, out IDictionary<string, object> additionalOutputs, TimeSpan timeout); |
流程 |
public sealed class addActivity :NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; }
protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); } } |
调用 |
static void staticCallActivityResult() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 1 }, { "Y", 2 } };
int result = WorkflowInvoker.Invoke(new addActivity(), p);
System.Console.WriteLine(result);
} |
结果 |
流程 |
|
调用 |
static void staticCallOutArgument() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 3 }, { "Y", 4 } };
System.Collections.Generic.IDictionary<string, object> outArgument = WorkflowInvoker.Invoke(new testWorkflow(), p);
foreach (var item in outArgument) { System.Console.WriteLine(item); }
} |
结果 |
流程 |
public sealed class resultAndOutArgumentActivity : NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; } public OutArgument<int> myValue { set; get; } protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); context.SetValue(myValue, x + y); } } |
调用 |
static void staticCallResultAndOutArgument() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 5 }, { "Y", 6 } };
System.Collections.Generic.IDictionary<string, object> outArgument; int result = WorkflowInvoker.Invoke(new resultAndOutArgumentActivity(), p,out outArgument,new TimeSpan(0));
System.Console.WriteLine(result);
foreach (var item in outArgument) { System.Console.WriteLine(item); }
} |
结果 |
WorkflowInvoker实例方式调用工作流时,有返回值Activity的返回值与OutArgument都由 System.Collections.Generic.Dictionary<string, object>集合返回
流程 |
public sealed class addActivity :NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; }
protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); } } |
调用 |
static void callActivityResult() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 7 }, { "Y", 8 } };
WorkflowInvoker invoker = new WorkflowInvoker(new addActivity()); System.Collections.Generic.IDictionary<string, object> result = invoker.Invoke(p);
foreach (var item in result) { System.Console.WriteLine(item); }
} |
结果 |
流程 |
|
调用 |
static void callOutArgument() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 9 }, { "Y", 10 } };
WorkflowInvoker invoker = new WorkflowInvoker(new testWorkflow());
System.Collections.Generic.IDictionary<string, object> outArgument = invoker.Invoke(p);
foreach (var item in outArgument) { System.Console.WriteLine(item); } } |
结果 |
流程 |
public sealed class resultAndOutArgumentActivity : NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; } public OutArgument<int> myValue { set; get; } protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); context.SetValue(myValue, x + y); } } |
调用 |
static void callResultAndOutArgument() { var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 11 }, { "Y", 12 } };
WorkflowInvoker invoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
System.Collections.Generic.IDictionary<string, object> outArgument;
outArgument = invoker.Invoke(p);
foreach (var item in outArgument) { System.Console.WriteLine(item); }
} |
结果 |
流程 |
public sealed class resultAndOutArgumentActivity : NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; } public OutArgument<int> myValue { set; get; } protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); context.SetValue(myValue, x + y); } } |
调用 |
#region //async static void async() { WorkflowInvoker myInvoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 13 }, { "Y", 14 } };
myInvoker.InvokeCompleted += new EventHandler<InvokeCompletedEventArgs>(myInvoker_InvokeCompleted);
myInvoker.InvokeAsync(p);
}
static void myInvoker_InvokeCompleted(object sender, InvokeCompletedEventArgs e) { foreach (var item in e.Outputs) { System.Console.WriteLine(item); } } #endregion |
结果 |
流程 |
public sealed class resultAndOutArgumentActivity : NativeActivity<int> { public InArgument<int> X { get; set; } public InArgument<int> Y { get; set; } public OutArgument<int> myValue { set; get; } protected override void Execute(NativeActivityContext context) { int x = X.Get(context); int y = Y.Get(context);
context.SetValue(base.Result, x + y); context.SetValue(myValue, x + y); } } |
调用 |
#region //begion end static void beginEnd() { WorkflowInvoker myInvoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 15 }, { "Y", 16 } };
IAsyncResult ir = myInvoker.BeginInvoke(p, new AsyncCallback(callback), myInvoker);
System.Collections.Generic.IDictionary<string, object> outArgument = myInvoker.EndInvoke(ir);
foreach (var item in outArgument) { System.Console.WriteLine(item); }
} static void callback(IAsyncResult asyncResult) { System.Threading.ManualResetEvent mr = asyncResult.AsyncWaitHandle as System.Threading.ManualResetEvent;
System.Console.WriteLine("callback"); }
#endregion |
结果 |
// WorkflowInvoker invoker = new WorkflowInvoker(new testWorkflow());
// invoker.Extensions.Add(myTracking);
// invoker.Invoke(); |
具体见[Tracking 跟踪] |