entity-framework-corestate-machinemasstransitsagaautomatonymous

State machine inconsistency while using Automatonymous


I am new to microservices and using masstransit with automatonymous. Currently my state machine is showing inconsistency while execution. The code inside Initially works as expected but the control flow does not execute the code inside During after the second event has completed. This happens only for 1 state machine, all the other state machine works as expected. When I move the code inside During to Initially(commented code in initially), it works fine. Below is how my state machine looks like:

 InstanceState(x => x.CurrentState);
        Event(() => ServiceRequest1Registered, x => x.CorrelateById(context => context.Message.AggregateId));
        Event(() => ServiceRequest2Registered, x => x.CorrelateById(context => context.Message.AggregateId));

        Initially(
           When(ServiceRequest1Registered, 
           context => context.Data.ServiceTypeId != (int)ServiceType.IndividualService)
           .Then(context => _logger.LogInformation($"When Initially, ServiceRequest1Registered and wrong condition"))
           .Finalize(),
           When(ServiceRequest1Registered, 
           context => context.Data.ServiceTypeId == (int)ServiceType.IndividualService)
           .Then(context => _logger.LogInformation($"When Initially and ServiceRequest1Registered"))
           .Send(url,
               x => new ServiceRequest2RegisteredCommand
               {
                   InitiatedBy = x.Instance.InitiatedBy,
                   ServiceRequestId = x.Instance.Id,
                   Schedules = _mapper.Map<List<ScheduleDTO>>(x.Data.ServiceRequest1Schedules)
               })
           .Then(context => _logger.LogInformation($"Send ServiceRequest2RegisteredCommand")
           .TransitionTo(ServiceRequest1RegisterCompleted)
           ////When(ServiceRequest2Registered)
           ////.Then(context => _logger.LogInformation($"When ServiceRequestRegister2Completed and ServiceRequest2Registered"))
           //// .Finalize())
           );

        During(ServiceRequestRegister1Completed,
           Ignore(ServiceRequest1Registered),
           When(ServiceRequest2Registered)
           .Then(context => _logger.LogInformation($"When ServiceRequestRegister1Completed and ServiceRequest2Registered"))
            .Finalize());

        SetCompleted(async instance =>
        {
            State<ServiceRequestState> currentState = await this.GetState(instance);

            _logger.LogInformation($"Final state : {ServiceRequest2Registered.Equals(currentState)}");
            return ServiceRequest2Registered.Equals(currentState);
        });

Masstransit setup with RabbitMQ below

services.AddMassTransit(x =>
        {
            x.AddBus(provider => MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.Host(hostUri, hst =>
                {
                    hst.Username(appSettings.RabbitMQ.Username);
                    hst.Password(appSettings.RabbitMQ.Password);
                });

                cfg.ReceiveEndpoint("microservice-response", e =>
                {
                    e.UseInMemoryOutbox();
                    AddConsumers(e, provider);
                    e.ConfigureSaga<ServiceRequestRegisterState>(provider);
                });
            }));

            x.AddSagaStateMachine<ServiceRequestRegisterStateMachine, ServiceRequestRegisterState>()
               .InMemoryRepository();
        });

        services.AddSingleton<IHostedService, MassTransitBusService>();

I tried both SetCompleted and SetCompletedWhenFinalized(). We are using Masstransit v6.2.1 with automatonymous v4.2.1. Need help to identify why we have this issue or if there is something wrong with implementation?


Solution

  • Correlation Id was different for 2nd event. This resulted in the 2nd event hitting initially. Corrected that issue and rest all worked. Ensure all events in the statemachine has the same correlationid and that this correlationid does not change during the orchestration.