博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4:活动关联Activity correlation

Posted on 2009-12-06 16:43  生鱼片  阅读(1665)  评论(0编辑  收藏  举报

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<string>();
    var handel = new Variable<CorrelationHandle>();
 
    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<int>(42)}
            }
        }
    };
 
    var receive = new ReceiveReply()
    {
        Request = send,
        Content = new ReceiveParametersContent
        {
            Parameters =
            {
                {"GetDataResult", new OutArgument<string>(getDataResult)}
            }
        }
    };
 
    var workflow = new Sequence()
    {
        Variables = { getDataResult, handel },
        Activities = 
        {
            send, 
            receive,
            new WriteLine() { Text = getDataResult } 
        }
    };
 
    return workflow;
}

 

上面我们是使用RequestReplyCorrelationInitializer,我们还可以使用CorrelationScope来完成同样的事情,创建同样的工作流,如下:

static Activity CreateWorkflow()
{
    var getDataResult = new Variable<string>();
 
    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<int>(42)}
            }
        }
    };
 
    var receive = new ReceiveReply()
    {
        Request = send,
        Content = new ReceiveParametersContent
        {
            Parameters =
            {
                {"GetDataResult", new OutArgument<string>(getDataResult)}
            }
        }
    };
 
    var workflow = new Sequence()
     {
         Variables = { getDataResult },
         Activities = 
        {  
            new CorrelationScope() 
            {
                Body = new Sequence() 
                { 
                    Activities = { send, receive } 
                } 
            }, 
            new WriteLine() { Text = getDataResult } }
     };
 
    return workflow;
}