随笔 - 234, 文章 - 12, 评论 - 1671, 阅读 - 74万
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4:活动关联Activity correlation

Posted on   生鱼片  阅读(1666)  评论(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;
}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示