《WF编程》系列之20 - 触发事件与调用方法:宿主的实现 3.2.3.4 宿主的实现
《WF编程》系列之20 - 触发事件与调用方法:宿主的实现
3.2.3.4 宿主的实现
最后,我们需要编写一个宿主来执行工作流.
WorkflowRuntime runtime = new WorkflowRuntime();
ExternalDataExchangeService dataService;
dataService = new ExternalDataExchangeService();
runtime.AddService(dataService);
BugFlowService bugFlow = new BugFlowService();
dataService.AddService(bugFlow);
AutoResetEvent waitHandle = new AutoResetEvent(false);
runtime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
runtime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { waitHandle.Set(); };
WorkflowInstance instance;
instance = runtime.CreateWorkflow(typeof(BugFlow));
instance.Start();
Bug bug = new Bug(1, "Bug Title", "Bug Description");
bugFlow.CreateBug(instance.InstanceId, bug);
waitHandle.WaitOne();
ExternalDataExchangeService dataService;
dataService = new ExternalDataExchangeService();
runtime.AddService(dataService);
BugFlowService bugFlow = new BugFlowService();
dataService.AddService(bugFlow);
AutoResetEvent waitHandle = new AutoResetEvent(false);
runtime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
runtime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { waitHandle.Set(); };
WorkflowInstance instance;
instance = runtime.CreateWorkflow(typeof(BugFlow));
instance.Start();
Bug bug = new Bug(1, "Bug Title", "Bug Description");
bugFlow.CreateBug(instance.InstanceId, bug);
waitHandle.WaitOne();
下面是与宿主程序对话的步骤:
1. 宿主创建了ExternalDataExchangeService并将其加入到工作流Runtime中.这个服务由Windows Workflow提供,用以管理工作流Runtime中的所有本地通信服务,并且将作为BugFlowService的容器.
2. 宿主创建了BugFlowService的实例并将其加入到ExternalDataExchangeService管理的服务列表中. ExternalDataExchangeService将分析IBugFlowService接口,准备s操作BugAdded事件和调用AssignBug方法.
3. 宿主创建了BugFlow工作流的实例并使其运行.工作流实例将等待BugFlow事件的触发.
4. 宿主创建了新的Bug对象,并将其传递给bugFlow的CreateBug方法.
CreateBug方法将触发BugAdded事件.工作流Runtime(凭借数据交换服务的帮助)将捕捉到此事件.执行一些处理,并传递事件给工作流实例.工作流实例也许已经等待了数分钟,数小时,数日甚至更久.当此事件触发时工作流或许已经不在内存里;Runtime或许已经将其持久化到了数据库中.通过监听事件,工作流Runtime有机会在事件传递到达之前去将工作流加载到内存中.接着工作流就可以调用AssignBug方法.
触发事件与调用方法的范例下载:Chapter3_LCS.zip
http://xiaoshatian.cnblogs.com/