WF4:活动关联Activity|WF4:活动关联Activity correlation

Activity correlation:活动关联,确保两个活动一起工作。
当使用多个活动来完成单个操作时需要使用Activity correlation,WF4中的Send和ReceiveReply活动就是很好的例子。当我们使用ReceiveAndSendReply 或 SendAndReceiveReply 模板时,需要的活动的关联都已经自动的配置好了,但是如果我们在代码中使用Send 和 ReceiveReply或者 Receive 和SendReply活动时,就需要自己手动去实现,如没有改关联会有下面异常:System.InvalidOperationException:
The Send activity is configured with a request/reply Operation 'GetData', however, there is no ReceiveReply activity paired with it. Please pair Send with ReceiveReply and correlate them using a CorrelationHandle.
有不同的方式来配置活动关联,第一种就是使用CorrelationScope活动,CorrelationScope会自动的创建所需要的关联句柄,并且消息活动将会使用这个句柄。
下面是例子:



static Activity CreateWorkflow()

{

var getDataResult = new Variable();

var handel = new Variable();


var send = new Send()

{

CorrelationInitializers =

{

new RequestReplyCorrelationInitializer()

{

CorrelationHandle = handel

}

},

OperationName = "GetData",

ServiceContractName = "IService1",

Endpoint = new Endpoint()

{

Binding = new BasicHttpBinding(),

AddressUri = new Uri("http://localhost:8080/GetDataService/")

},

Content = new SendParametersContent()

{

Parameters =

{

{"value", new InArgument(42)}

}

}

};


var receive = new ReceiveReply()

{

Request = send,

Content = new ReceiveParametersContent

{

Parameters =

{

{"GetDataResult", new OutArgument(getDataResult)}

}

}

};


var workflow = new Sequence()

{

Variables = { getDataResult, handel },

Activities =

{

send,

receive,

new WriteLine() { Text = getDataResult }

}

};


return workflow;

}






【WF4:活动关联Activity|WF4:活动关联Activity correlation】上面我们是使用RequestReplyCorrelationInitializer,我们还可以使用CorrelationScope来完成同样的事情,创建同样的工作流,如下:
static Activity CreateWorkflow()

{

var getDataResult = new Variable();


var send = new Send()

{

OperationName = "GetData",

ServiceContractName = "IService1",

Endpoint = new Endpoint()

{

Binding = new BasicHttpBinding(),

AddressUri = new Uri("http://localhost:8080/GetDataService/")

},

Content = new SendParametersContent()

{

Parameters =

{

{"value", new InArgument(42)}

}

}

};


var receive = new ReceiveReply()

{

Request = send,

Content = new ReceiveParametersContent

{

Parameters =

{

{"GetDataResult", new OutArgument(getDataResult)}

}

}

};


var workflow = new Sequence()

{

Variables = { getDataResult },

Activities =

{

new CorrelationScope()

{

Body = new Sequence()

{

Activities = { send, receive }

}

},

new WriteLine() { Text = getDataResult } }

};


return workflow;

}本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/12/06/wf4-Activitycorrelation.html,如需转载请自行联系原作者

    推荐阅读