WF3.5 的SendActivity、ReceiveActivity与WorkflowServiceHost (3)

ReceiveActivity

 

  1. 如果工作流需要实现 WCF 服务协定中定义的操作,可使用 ReceiveActivity
  2. ReceiveActivity 可以实现已定义的服务协定中的操作,
  3. ReceiveActivity也可以在工作流本身中定义服务协定和操作。称为工作流优先协定。
  4. 要使用ReceiveActivity,必须全使用WorkflowServiceHost
  5. ReceiveActivity 支持一组预定义的消息交换模式。这些模式包括:

单向接收

发送消息的客户端不需要该服务发送响应

接收请求 -发送响应

接收并处理一条消息后,会向客户端发回一个响应

接收请求 - 发送错误

接收并处理一条消息后,会向客户端发回一个响应或错误

 

静态方法

GetContext

返回一个字典,该字典包含用于在客户端和工作流服务之间进行通信的上下文信息

GetRootContext

返回与给定活动实例关联的根上下文的上下文信息

GetWorkflowServiceAttributes

返回由 ReceiveActivity 活动实现的服务的属性。这些属性包括多项,如 AddressFilterModeConfigurationNameIncludeExceptionDetailInFaults 和是否在从 ReceiveActivity 活动返回的任何错误中包括异常详细信息。

SetWorkflowServiceAttributes

设置由 ReceiveActivity 活动实现的服务的属性。这些属性包括多项,如 AddressFilterModeConfigurationName 和是否在从 ReceiveActivity 活动返回的任何错误中包括异常详细信息。

 

属性

 

ServiceOperationInfo

实现的协定和服务操作

FaultMessage

接收活动完成执行时返回的异常

ParameterBindings

服务的方法参数的集合

Context

上下文信息

ContextToken

表示一个可用于指定上下文的令牌,应当使用该令牌来关联 ReceiveActivity 和该活动与之通信的客户端之间的交换

 

每个 ContextToken 均在所有者活动的范围内定义。可以建立名称相同但所有者范围不同的两个不同的上下文令牌模型。与这些上下文令牌关联的上下文值在运行时将有所不同。

 

默认情况下,每个工作流都具有一个隐式根上下文令牌,其范围是根活动级别。

根上下文令牌的名称为"(RootContext)"

OwnerActivityName null

如果 ReceiveActivity 没有与其关联的 ContextToken,则将使用根上下文令牌。

CanCreateInstance

获取或设置操作是否将导致创建新的工作流服务实例

  1. 设置为 true ,表示一个服务操作,当不作为对话的一部分由服务客户端调用时,服务将创建工作流的新实例。
  2. 设置为 false,客户端无法使用服务操作调用来创建工作流的新实例,只能使用关联的 WorkflowRuntime 对象的 CreateWorkflow 方法可以创建这样的工作流,并作为对话的一部分由服务客户端调用
  3. 默认值为 false

 

ReceiveActivity 服务模式

 

CanCreateInstance设置为 true ,表示一个服务操作,当不作为对话的一部分由服务客户端调用时,服务将创建工作流的新实例。

工作流服务

WCF接口

[ServiceContract]

public interface wxdIService

{

 

[OperationContract]

int add(int v1,int v2);

 

[OperationContract]

string getwxd(string v);

 

}

<system.serviceModel>

<services>

<service name="WorkflowConsoleApplication2.Workflow1" behaviorConfiguration="wxdBehavior">

<host>

<baseAddresses>

<add baseAddress="http://localhost:5432/wxwinter/Workflow1/" />

</baseAddresses>

</host>

<endpoint address=""

binding="wsHttpContextBinding"

contract="WorkflowConsoleApplication2.wxdIService">

<identity>

<dns value="localhost"/>

</identity>

</endpoint>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="wxdBehavior" >

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

<serviceCredentials>

<windowsAuthentication

allowAnonymousLogons="false"

includeWindowsGroups="true" />

</serviceCredentials>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

 

工作流

1.添加ReceiveActivity

2.设置WCF服务接口

 

 

3.

 

宿主

namespace WorkflowConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

WorkflowServiceHost host = new WorkflowServiceHost(typeof(WorkflowConsoleApplication2.Workflow1));

 

System.Workflow.Runtime.WorkflowRuntime runtime;

runtime = host.Description.Behaviors.Find<WorkflowRuntimeBehavior>().WorkflowRuntime;

runtime.WorkflowCreated += new EventHandler<WorkflowEventArgs>(runtime_WorkflowCreated);

host.Open();

Console.WriteLine(host.BaseAddresses[0].ToString());

Console.Read();

host.Close();

}

 

static void runtime_WorkflowCreated(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowCreated:" + e.WorkflowInstance.InstanceId.ToString());

}

 

}

}

 

客户端

1.添加引用

2.Winform界面

ServiceReference1.wxdIServiceClient ser = new WindowsFormsApplication1.ServiceReference1.wxdIServiceClient();

private void button1_Click(object sender, EventArgs e)

{

int value = ser.add(int.Parse(textBox1.Text), int.Parse(textBox2.Text));

textBox3.Text = value.ToString();

}

 

private void button2_Click(object sender, EventArgs e)

{

string s = ser.getwxd(textBox4.Text);

textBox5.Text = s;

}

 

调用

 

 

例子:https://files.cnblogs.com/wxwinter/ReceiveActivity.rar

 

 

 

posted @ 2008-05-23 09:14  WXWinter(冬)  阅读(1717)  评论(2编辑  收藏  举报