要求-回復關聯性使用在 Receive/SendReply 配對中以實現工作流程服務中的雙向操作,並在另一個 Web服務的 Send/ReceiveReply 配對中調用雙向操作。 在 WCF 服務中叫用雙向作業時,服務可以是傳統的命令式程式代碼型 Windows Communication Foundation (WCF) 服務,也可以是工作流程服務。 若要使用要求-回復相互關聯,必須使用雙向系結,例如 BasicHttpBinding。 不論叫用或實作雙向作業,相互關聯初始化步驟都類似,本節會涵蓋這些步驟。
在 Two-Way 作業中使用相關性並進行接收/回覆操作
配對 Receive/SendReply 可用來在工作流程服務中實作雙向作業。 執行環境使用請求與回覆的關聯性,以確保回覆分派給正確的呼叫者。 當使用 WorkflowServiceHost 裝載工作流程時,也就是針對工作流程服務的情況,則預設的關聯初始化已足夠。 在此案例中,工作流程會使用配對 Receive/SendReply ,不需要特定的相互關聯設定。
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder"
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
// Construct a workflow using StartOrder and ReplyToStartOrder.
明確初始化 Request-Reply 關聯性
如果其他雙向作業平行,則應該明確設定相互關聯。 這可以藉由指定 CorrelationHandle 和 RequestReplyCorrelationInitializer 來完成,或將 Receive/SendReply 放在 CorrelationScope 內。 在此範例中,要求-回復相互關聯是在配對 Receive/SendReply 上設定。
Variable<CorrelationHandle> RRHandle = new Variable<CorrelationHandle>();
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder",
CorrelationInitializers =
{
new RequestReplyCorrelationInitializer
{
CorrelationHandle = RRHandle
}
}
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
// Construct a workflow using StartOrder and ReplyToStartOrder.
您可以使用CorrelationScope活動,而不用明確設定相互關聯。 CorrelationScope 提供包含之傳訊活動的隱含 CorrelationHandle 。 在此範例中,一對Receive/SendReply包含在CorrelationScope中。 不需要明確的相互關聯設定。
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder"
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
CorrelationScope s = new CorrelationScope
{
Body = new Sequence
{
Activities =
{
StartOrder,
// Activities that create the reply.
ReplyToStartOrder
}
}
};
// Construct a workflow using the CorrelationScope.
如果需要其他相互關聯,則可以使用 CorrelationInitializers 所需 CorrelationInitializer
類型的個別傳訊活動的 屬性來設定它們。
在 Two-Way 作業中使用關聯搭配傳送/接收回覆
Receive 活動只能在由 WorkflowServiceHost 裝載的工作流程服務中使用,而 Send 和 Send/ReceiveReply 配對可以用於任何需要於 Web 服務上叫用方法的工作流程中。 如果使用 WorkflowServiceHost 裝載工作流程,則會套用上一節中所述的預設相互關聯。如果不是這樣,則必須透過 CorrelationInitializer 和 CorrelationHandle 明確設定相互關聯,或者通過 CorrelationScope 的隱式處理管理來設定相互關聯。
在具有雙向作業的服務上使用 新增服務參考 時,會產生活動,以明確指定要求/回復的相互關聯,並在內部將 Send/ReceiveReply 配對活動包裝起來。