相关概述Correlation Overview

相关是指使工作流服务消息彼此相关或与应用程序实例状态相关的机制,例如,使答复与初始请求相关,或者使特定订单 ID 与订单处理工作流的保留状态相关。Correlation is the mechanism for relating workflow service messages to each other or to the application instance state, such as a reply to an initial request, or a particular order ID to the persisted state of an order-processing workflow. 本主题概述了相关。This topic provides an overview of correlation. 本节中的其他主题提供了相关的各种类型的附加信息。The other topics in this section provide additional information for each type of correlation.

相关的类型Types of Correlation

相关可以基于协议,也可以基于内容。Correlation can be protocol-based or content-based. 基于协议的相关使用消息传递基础结构提供的数据在消息之间提供映射。Protocol-based correlations use data provided by the message delivery infrastructure to provide the mapping between messages. 使用基于协议的相关而彼此相关的消息通过使用内存中的对象(如 RequestContext)或通过传输协议提供的标记而彼此相关。Messages that are correlated using protocol-based correlation are related to each other using an object in memory, such as a RequestContext, or by a token provided by the transport protocol. 基于内容的相关使用应用程序特定数据使消息彼此相关。Content-based correlations relate messages to each other using application-specified data. 使用基于内容的相关而彼此相关的消息通过消息中某些由应用程序定义的数据(如客户编号)彼此相关。Messages that are correlated using content-based correlation are related to each other by some application-defined data in the message, such as a customer number.

参与相关的活动使用 CorrelationHandle 将消息传递活动绑定在一起。Activities that participate in correlation use a CorrelationHandle to tie the messaging activities together. 例如,Send(用于调用服务)和后续的 Receive(用于接收服务发送的回调)共享同一 CorrelationHandleFor example, a Send that is used to call a service and a subsequent Receive that is used to receive a callback from the service, share the same CorrelationHandle. 无论相关是基于内容还是基于协议,都会使用此基本模式。This basic pattern is used whether the correlation is content based or protocol based. 可以在每个活动上显式设置相关句柄,也可以将活动包含在 CorrelationScope 活动中。The correlation handle can be explicitly set on each activity or the activities can be contained in a CorrelationScope activity. CorrelationScope 中包含的活动的相关句柄由 CorrelationScope 管理,这些活动不需要显式设置 CorrelationHandleActivities contained in a CorrelationScope have their correlation handles managed by the CorrelationScope and do not require the CorrelationHandle to be explicitly set. CorrelationScope 范围为请求-答复相关提供 CorrelationHandle 管理,并提供一个附加相关类型。A CorrelationScope scope provides CorrelationHandle management for a request-reply correlation and one additional correlation type. 使用 WorkflowServiceHost 承载的工作流服务与 CorrelationScope 活动具有相同的默认相关管理。Workflow services hosted using WorkflowServiceHost have the same default correlation management as the CorrelationScope activity. 此默认相关管理通常意味着在许多情况下,CorrelationScope 或工作流服务中的消息传递活动不需要设置其 CorrelationHandle,除非有多个消息传递活动并行或重叠,例如,有两个并行 Receive 活动,或两个 Send 活动后跟两个 Receive 活动。This default correlation management generally means that in many scenarios, messaging activities in a CorrelationScope or a workflow service do not require their CorrelationHandle set unless multiple messaging activities are in parallel or overlap, such as two Receive activities in parallel, or two Send activities followed by two Receive activities. 本节中的主题涵盖每种特定的相关类型,并提供了有关默认相关的更多信息。More information about default correlation is provided in the topics in this section that cover each specific type of correlation. 有关消息传递活动的详细信息,请参阅 消息传递活动如何:使用消息传递活动创建工作流服务For more information about messaging activities see Messaging Activities and How to: Create a Workflow Service with Messaging Activities.

基于协议的相关Protocol-Based Correlation

基于协议的相关使用传输机制使消息彼此相关或与相应实例相关。Protocol-based correlation uses the transport mechanism to relate messages to each other and the appropriate instance. 系统提供的一些协议相关包括请求-答复相关和基于上下文的相关。Some system-provided protocol correlations include request-reply correlation and context-based correlation. 请求-答复相关用于将一对消息传递活动相关以形成双向操作,如 SendReceiveReply 成对,或 ReceiveSendReply 成对。A request-reply correlation is used to correlate a single pair of messaging activities to form a two-way operation, such as a Send paired with a ReceiveReply, or a Receive paired with a SendReply. Visual Studio 工作流设计器还提供了一组活动模板来快速实现此模式。The Visual Studio Workflow Designer also provides a set of activity templates to quickly implement this pattern. 基于上下文的相关基于 .Net 上下文交换协议规范中描述的上下文交换机制。A context-based correlation is based on the context exchange mechanism described in the .NET Context Exchange Protocol Specification. 若要使用基于上下文的相关,必须在终结点上使用诸如 BasicHttpContextBindingWSHttpContextBindingNetTcpContextBinding 之类的基于上下文的绑定。To use context-based correlation, a context-based binding such as BasicHttpContextBinding, WSHttpContextBinding or NetTcpContextBinding must be used on the endpoint.

有关协议关联的详细信息,请参阅 持久双工请求-答复For more information about protocol correlation, see Durable Duplex and Request-Reply. 有关使用 Visual Studio 工作流设计器活动模板的详细信息,请参阅 消息传递活动For more information about using the Visual Studio Workflow Designer activity templates, see Messaging Activities. 有关示例代码,请参阅 NetContextExchangeCorrelation 示例。For sample code, see the NetContextExchangeCorrelation sample.

基于内容的相关Content-Based Correlation

基于内容的相关使用消息中的某些信息片段将消息与特定实例关联。Content-based correlation uses some piece of information in the message to associate it to a particular instance. 与基于协议的相关不同,基于内容的相关要求应用程序作者显式声明此数据在各相关消息中的位置。Unlike protocol-based correlation, content-based correlation requires the application author to explicitly state where this data can be found in each related message. 使用基于内容相关的活动通过使用 MessageQuerySet 指定此消息数据。Activities that use content-based correlation specify this message data by using a MessageQuerySet. 与不使用某个上下文绑定(如 BasicHttpContextBinding)的服务进行通信时,基于内容的相关非常有用。Content-based correlation is useful when communicating with services that do not use one of the context bindings such as BasicHttpContextBinding.

请参阅See also