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();
}
If I catch an exception in HandleCrawlRequestCreated, I raise CrawlFailed, like so:
context.Raise(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?