I have an Automatonymous state machine with a bunch of events. I would like to publish each event after the previous one completes. I know I could publish it in the consumer of each event, but some of them might be reused in different state machines so I would like to declare it in the state machine definition. Here's what I have now:
public class TestStateMachine : MassTransitStateMachine<TestState>
{
public State StepOneCompleted { get; set; }
public State StepTwoCompleted { get; set; }
public State StepThreeCompleted { get; set; }
public Event<ProcessStepOne> ProcessStepOne { get; set; }
public Event<ProcessStepTwo> ProcessStepTwo { get; set; }
public Event<ProcessStepThree> ProcessStepThree { get; set; }
public TestStateMachine()
{
InstanceState(x => x.CurrentState);
Initially(When(ProcessStepOne)
.Publish(context => new ProcessStepTwo
{
CorrelationId = context.Data.CorrelationId,
})
.TransitionTo(StepOneCompleted));
During(StepOneCompleted,
When(ProcessStepTwo)
.Publish(context => new ProcessStepThree
{
CorrelationId = context.Data.CorrelationId,
})
.TransitionTo(StepTwoCompleted));
During(StepTwoCompleted, When(ProcessStepThree).TransitionTo(StepThreeCompleted));
}
}
With this definition second step gets published while step one is still being executed and third step while second step is being executed. I would like to make sure that step one completes, then step two gets published, then step two completes and step three gets published. I tried to use WhenLeave
behaviour but it expects a State, not an Event so I don't have access to context.Data
and CorrelationId will not be only variable that I will pass between steps.
Is there a possible way to achieve that within state machine definition?