了解“最终确定"在大众运输中 [英] Understanding "Finalize" in MassTransit

查看:53
本文介绍了了解“最终确定"在大众运输中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解Finalize()在MassTransit中的工作方式时遇到了麻烦,尤其是在初始状态下是否可以执行它.设置:

I'm having some trouble understanding how Finalize() works in MassTransit, and specifically whether it can be executed during the initial state. Setup:

public Event<ICrawlRequestCreated> CrawlCreated { get; private set; }
public Event CrawlFailed { get; private set; }

public State Executing { get; private set; }
public State Completed { get; private set; }
public State Failed { get; private set; }

public WorkflowSaga()
{
    InstanceState(x => x.CurrentState);

    Initially(
        When(CrawlCreated)
            .Then(HandleCrawlRequestCreated)
            .TransitionTo(Executing),
        When(CrawlFailed)
            .Then(HandleCrawlFailed)
            .TransitionTo(Failed)
            .Finalize()
    );

    ...

    SetCompletedWhenFinalized();
}

如果我在HandleCrawlRequestCreated中捕获到异常,则会引发CrawlFailed,如下所示:

If I catch an exception in HandleCrawlRequestCreated, I raise CrawlFailed, like so:

context.Raise(CrawlFailed);

可正确触发HandleCrawlFailed,但不会从存储库(通过EF的SQL Server)中删除状态机实例.但是,如果在执行状态期间引发CrawlFailed,则实例将从存储库中删除.我想念什么?

which fires HandleCrawlFailed correctly, but it doesn't remove the state machine instance from the repository (SQL Server via EF). But if I raise CrawlFailed during the executing state, the instance is removed from the repository. What am I missing?

推荐答案

您应该使用.Catch()方法,而不是在方法中捕获异常,并且在.Catch中,您可以完成而不是过渡到执行状态.

You should use the .Catch() methods, instead of catching the exception in your method, and within the .Catch you can finalize instead of transitioning to the Executing state.

如果您在Initial中进行Finalize,则永远不要将状态机持久化到数据库中,但是我没有编写EF存储库,并且我不确定测试范围是否会确保这种情况.

If you Finalize in Initial, it shouldn't ever persist the state machine to the database, but I didn't write the EF repository and I'm not sure the test coverage ensures that to be the case.

这篇关于了解“最终确定"在大众运输中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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