Combine to Whens in Automatonymous state machine

非 Y 不嫁゛ 提交于 2019-12-10 22:48:51

问题


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.


回答1:


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));
}


来源:https://stackoverflow.com/questions/36177310/combine-to-whens-in-automatonymous-state-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!