CallExternalMethodActivity 活动和 HandleExternalEventActivity 活动可用于与本地服务进行输入和输出通信。 您可以直接使用这些活动进行一般通信,也可以创建 CallExternalMethodActivity 和 HandleExternalEventActivity 类的子类以创建一些活动,这些活动严格绑定到具有 ExternalDataExchangeAttribute 属性的接口上的特定事件和方法。
CallExternalMethodActivity 基类调用由向 WorkflowRuntime 注册的相应本地服务的 InterfaceType 和 MethodName 属性指定的方法。 此调用是使用从绑定位置的 ParameterBindings 集合中收集的参数以同步方式执行的。 如果该方法具有返回值,则会在活动执行完毕前将这些值设置为绑定位置。
1.定义一个接口:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Workflow.Activities;namespace CallExternalMethodActivityExample{ [ExternalDataExchange] public interface IExternalMethod { void returnName(String name); }}
2.定义接口的实现类:
namespace CallExternalMethodActivityExample{ public class ExternalMethodService:IExternalMethod { public void returnName(String name) { Console.WriteLine("the returnName method has be runned"); } }}
3.向顺序工作流中拖入CallExternalMethod,并为其指定接口和方法。
4.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Workflow.Runtime;using System.Workflow.Runtime.Hosting;using System.Workflow.Activities;namespace CallExternalMethodActivityExample{ class Program { static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; ExternalDataExchangeService exchangeService = new ExternalDataExchangeService(); workflowRuntime.AddService(exchangeService); ExternalMethodService emService = new ExternalMethodService(); exchangeService.AddService(emService); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(CallExternalMethodActivityExample.Workflow1)); instance.Start(); waitHandle.WaitOne(); } } }}
相关资源:敏捷开发V1.0.pptx