我在理解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,但不会从存储库(通过EF的SQL Server)中删除状态机实例。但是,如果在执行状态期间引发CrawlFailed,则实例将从存储库中删除。我想念什么?

最佳答案

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

如果您在Initial中完成,则永远不要将状态机持久化到数据库中,但是我没有编写EF信息库,并且不确定测试范围是否会如此。

10-04 22:22