在自动命名状态机中结合到何时 [英] Combine to Whens in Automatonymous state machine

查看:110
本文介绍了在自动命名状态机中结合到何时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从MassTransit状态机传奇发出请求,并等待答复.

I am making a Request from MassTransit state machine saga and wait for reply.

但是可能会出现两个错误:

But there could be two errors coming back to me:

  • MyRequest.TimeoutExpired
  • MyRequest.Faulted

我不在乎请求没有满足的条件,我希望两种情况都导致发布错误消息.

I don't care on which conditions the request was not fulfilled, I want both situations to result in an error message to be published.

但是,我找不到任何将两个结果与条件结合起来的方法,因此我可以为两个结果提供一个处理案例,而不必复制粘贴我的代码.

However, I could not find any way to combine two outcomes with or condition, so I can have one handling case for both outcomes and not copy-paste my code.

推荐答案

在这种情况下,您应该创建一个自定义活动(高级的,可能不是必需的),或者只创建一个从两个While()条件中调用的方法,这样您就可以在语句之间重用行为.

In this case, you should either create a custom activity (advanced, probably not necessary) or just create a method that is called from both When() conditions, so that you can reuse the behavior between statements.

Task PublishEvent(BehaviorContext<TInstance> context)
{
    var consumeContext = context.GetPayload<ConsumeContext>();

    return consumeContext.Publish(new MyEvent(...));
}

{
    During(MyRequest.Pending,
        When(MyRequest.Completed)
            .ThenAsync(PublishEvent),
        When(MyRequest.Faulted)
            .ThenAsync(PublishEvent));
}

这篇关于在自动命名状态机中结合到何时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆