2

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?

Isard
  • 312
  • 1
  • 14
  • 3
    If you are following steps as part of a transaction with compensation, you really should be using a [routing slip](https://masstransit-project.com/advanced/courier/). Routing slips are designed for sequential activities, support compensation, and allow event subscriptions to monitor success, failure, progress. – Chris Patterson May 26 '21 at 11:59
  • Alright, I guess that's a point for routing slip. I'll go with it then, thanks. – Isard May 28 '21 at 10:34

0 Answers0