理解"敲定"在MassTransit

时间:2017-08-23 19:10:50

标签: c# .net masstransit

我很难理解Finalize()在MassTransit中是如何工作的,特别是它是否可以在初始状态下执行。设置:

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:

context.Raise(CrawlFailed);

正确触发HandleCrawlFailed,但它不会从存储库中删除状态机实例(SQL Server通过EF)。但是如果我在执行状态期间引发CrawlFailed,则会从​​存储库中删除该实例。我错过了什么?

1 个答案:

答案 0 :(得分:3)

您应该使用.Catch()方法,而不是在方法中捕获异常,并且在.Catch中,您可以最终确定而不是转换到执行状态。

如果你在Initial中终结,那么它不应该永久保存状态机到数据库,但我没有编写EF存储库,我不确定测试覆盖率是否确保如此。