1. Windows Workflow Foundation 通信服务使工作流能够使用方法和事件通过消息与外部系统交互。
l 事件用于将数据发送到工作流,而工作流使用方法将数据发送到主机应用程序。
l 通过事件与工作流进行通信的功能提供了一种将数据发送到工作流的异步方式。
图一
2. 基本的通讯行为
• 接口定义
– 在工作流和应用之间进行通讯
• CallExternalMethod Activity
– 从工作流发送数据到应用程序(Workflow->App)见A
• HandleExternalEvent Activity
– 从应用程序发送数据到工作流(App->Workflow)见B
图二
A:本地通讯服务
图三
[DataExchangeService]
public interface IHelpRequest
{
void OnRequestEscalated(string id);
event EventHandler<HelpRequestArgs> CloseRequest;
}
工作流通讯的驻留需求
• 实现接口的定义
– 当工作流调用驻留程序的时候,该功能将被执行
– 提供帮助功能从驻留程序中发布工作流事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;
using System.Windows.Forms;
using System.Workflow.Runtime;
namespace 工作流调用外部应用程序
{
class InvokeExternalMethod:Iconnetor
{
public event EventHandler<ConnetorEvnetArgs> Approve;
public event EventHandler<ConnetorEvnetArgs> Reject;
public void CreatM(string Pro)
{
Console.WriteLine("调用应用程序方法开始"+WorkflowEnvironment.WorkBatch.ToString());
ShowDialog(Pro);
}
void ShowDialog(string pro)
{
DialogResult result = MessageBox.Show("是否同意进行通讯", "", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Approve(null, new ConnetorEvnetArgs(WorkflowEnvironment.WorkflowInstanceId, pro));
}
else
Reject(null, new ConnetorEvnetArgs(WorkflowEnvironment.WorkflowInstanceId, pro));
}
}
[ExternalDataExchange]
interface Iconnetor
{
void CreatM(string Pro);
event EventHandler<ConnetorEvnetArgs> Approve;
event EventHandler<ConnetorEvnetArgs> Reject;
}
[Serializable]
public class ConnetorEvnetArgs : ExternalDataEventArgs
{
string _pro;
public ConnetorEvnetArgs(Guid instanceid, string pro):base(instanceid)
{
this._pro = pro;
}
public string Pro
{
get
{
return this._pro;
}
}
}
}
• 向运行时注册执行实例
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);
Iconnetor invokeService = new InvokeExternalMethod();
exchangeService.AddService(invokeService);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(工作流调用外部应用程序.Workflow1));
instance.Start();
waitHandle.WaitOne();
Console.ReadLine();
}
工作流
图四
B:Web服务
• Web服务发布
– 将工作流都发布为Web 服务
– WebServiceInputActivity
WebServiceOutputActivity
图五
Web服务调用
– InvokeWebServiceActivity
图六