WF提供了一组核心服务,例如在SQL 数据库中存储工作流实例的执行详细信息的持久性服务,计划服务,事务服务和跟踪服务。除了这些WF也提供了另外一种服务,叫做Local Service也可以叫做Data exchange service。主要是实现工作流和宿主程序之间的通信,使工作流能够使用方法和事件通过消息与外部系统交互。 事件用于将数据发送到工作流,而工作流使用方法将数据发送到主机应用程序。 通过事件与工作流进行通信的功能提供了一种将数据发送到工作流的异步方式。本文主要讲述调用外部方法的部分。
下面首先说说如何开发一个本地服务:
1.使用C#的接口定义服务契约,在接口中定义你方法和事件。并使用[ExternalDataExchangeAttribute]装饰该接口,用于说明这是一个本地服务的接口。
2.开发一个实现了该接口的类,用于实现你的逻辑。
3.创建一个工作流实例,并将该本地服务添加到工作流引擎中去。
1.定义一个Account类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WorkflowConsoleApplication5{ [Serializable] public class Account { private Int32 _id; private String _name = String.Empty; private Double _balance; public Int32 Id { get { return _id; } set { _id = value; } } public String Name { get { return _name; } set { _name = value; } } public Double Balance { get { return _balance; } set { _balance = value; } } }}
2.定义一个接口,需要ExternalDataExchange属性
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Workflow.Activities;namespace WorkflowConsoleApplication5{ [ExternalDataExchange] interface IAccountServices { Account AdjustBalance(Int32 id, Double adjustment); }}
3.实现该接口
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WorkflowConsoleApplication5{ class AccountService:IAccountServices { private Dictionary<Int32, Account> _accounts= new Dictionary<int, Account>(); public AccountService() { Account account = new Account(); account.Id = 101; account.Name = "Neil Armstrong"; account.Balance = 100.00; _accounts.Add(account.Id, account); } public Account AdjustBalance(Int32 id, Double adjustment) { Account account = null; if (_accounts.ContainsKey(id)) { account = _accounts[id]; account.Balance += adjustment; } return account; } }}
代码方式
在工作流中定义三个属性:
我们向工作流中拖入一个CodeActivity,Activity有一个方法OnActivityExecutionContextLoad(),我们通过该的IServiceProvider的GetService方法来获取本地服务
using System;using System.ComponentModel;using System.ComponentModel.Design;using System.Collections;using System.Drawing;using System.Linq;using System.Workflow.ComponentModel.Compiler;using System.Workflow.ComponentModel.Serialization;using System.Workflow.ComponentModel;using System.Workflow.ComponentModel.Design;using System.Workflow.Runtime;using System.Workflow.Activities;using System.Workflow.Activities.Rules;namespace WorkflowConsoleApplication5{ public sealed partial class Workflow1 : SequentialWorkflowActivity { private Int32 _id; private Double _adjustment; private Account _account; private IAccountServices _accountServices; public Int32 Id { get { return _id; } set { _id = value; } } public Double Adjustment { get { return _adjustment; } set { _adjustment = value; } } public Account Account { get { return _account; } set { _account = value; } } protected override void OnActivityExecutionContextLoad(IServiceProvider provider) { base.OnActivityExecutionContextLoad(provider); _accountServices = provider.GetService(typeof(IAccountServices)) as IAccountServices; if (_accountServices == null) { throw new InvalidOperationException("Unable to retrieve IAccountServices from runtime"); } } public Workflow1() { InitializeComponent(); } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Account = _accountServices.AdjustBalance(Id, Adjustment); System.Console.WriteLine(Id+" "+Adjustment); } }}
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 WorkflowConsoleApplication5{ class Program { static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { AutoResetEvent waitHandle = new AutoResetEvent(false); ExternalDataExchangeService dataservice = new ExternalDataExchangeService(); workflowRuntime.AddService(dataservice); AccountService accountService = new AccountService(); dataservice.AddService(accountService); Dictionary<String, Object> dic = new Dictionary<string, object>(); dic.Add("Id",2); dic.Add("Adjustment",23.25); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication5.Workflow1),dic); instance.Start(); waitHandle.WaitOne(); } } }}
相关资源:虚拟机应用程序