1
votes

While following Crm system Saga Example in Rebus documentation I am getting a ConcurrencyException with the following Details when handling the event LegalInfoAcquiredInFirstSystem and LegalInfoAcquiredInSecondSystem.

Error Details are as follows

message_id: 150d3d50-1de4-4a2f-bd60-660fc441412e delivery_mode: 2 headers:
rbs2-content-type: application/json;charset=utf-8 rbs2-corr-id: 0848197f-3ebb-442a-a80b-49c4c30dc0ca rbs2-corr-seq: 2 rbs2-error-details: System.AggregateException: 1 unhandled exceptions (Update of saga with ID 95395c60-cf2e-48da-89ff-fdf192ce53b9 did not succeed because someone else beat us to it) ---> Rebus.Exceptions.ConcurrencyException: Update of saga with ID 95395c60-cf2e-48da-89ff-fdf192ce53b9 did not succeed because someone else beat us to it at Rebus.SqlServer.Sagas.SqlServerSagaStorage.Update(ISagaData sagaData, IEnumerable1 correlationProperties) at Rebus.Sagas.LoadSagaDataStep.SaveSagaData(RelevantSagaInfo sagaDataToUpdate, Boolean insert) at Rebus.Sagas.LoadSagaDataStep.SaveSagaData(RelevantSagaInfo sagaDataToUpdate, Boolean insert) at Rebus.Sagas.LoadSagaDataStep.Process(IncomingStepContext context, Func1 next) at Rebus.Pipeline.Receive.ActivateHandlersStep.Process(IncomingStepContext context, Func1 next) at Rebus.Pipeline.Receive.HandleRoutingSlipsStep.Process(IncomingStepContext context, Func1 next) at Rebus.Pipeline.Receive.DeserializeIncomingMessageStep.Process(IncomingStepContext context, Func1 next) at Rebus.DataBus.ClaimCheck.HydrateIncomingMessageStep.Process(IncomingStepContext context, Func1 next) at Rebus.Pipeline.Receive.HandleDeferredMessagesStep.Process(IncomingStepContext context, Func1 next) at Rebus.Retry.FailFast.FailFastStep.Process(IncomingStepContext context, Func1 next) at Rebus.Retry.Simple.SimpleRetryStrategyStep.DispatchWithTrackerIdentifier(Func`1 next, String identifierToTrackMessageBy, ITransactionContext transactionContext, String messageId, String secondLevelMessageId) --- End of inner exception stack trace --- rbs2-intent: pub rbs2-msg-id: 150d3d50-1de4-4a2f-bd60-660fc441412e rbs2-msg-type: Crm.Messages.Events.LegalInfoAcquiredInFirstSystem, Crm.Messages.Events rbs2-return-address: RebusQueue rbs2-sender-address: RebusQueue rbs2-senttime: 2021-08-07T23:27:49.8601606+03:00 rbs2-source-queue: RebusQueue content_encoding: utf-8 content_type: application/json Payload 100 bytes Encoding: string {"$type":"Crm.Messages.Events.LegalInfoAcquiredInFirstSystem, Crm.Messages.Events","CorrId":"70001"}

My Rebus Configuration are as follows:

``

     public void ConfigureServices(IServiceCollection services)
     {

        AppSettings settings = new AppSettings();
        Configuration.Bind(settings);
        services.AutoRegisterHandlersFromAssemblyOf<AcquireLegalInformationFromFirstSystemHandler>();
        services.AddControllers();
        services.AddLogging(logging => logging.AddConsole());
        services.AddRebus((configure, serviceProvider) =>  configure
                .Transport(x =>
                {
                    x.UseRabbitMq($"amqp://{settings.Settings.UserName}:{settings.Settings.Password}@{settings.Settings.HostName}", settings.Settings.EndpointQueueName);

                })
                .Options(o => o.SetBusName("RebusSaga"))
                .Options(o => o.SimpleRetryStrategy(errorQueueAddress: settings.Settings.ErrorQueueName, maxDeliveryAttempts: 1, secondLevelRetriesEnabled: false))
                .Sagas(s => 
                {
                    s.StoreInSqlServer(settings.ConnectionStrings.RebusContext, "Sagas", "SagaIndex", true);
                    

                })
                .Timeouts(s => s.StoreInSqlServer(settings.ConnectionStrings.RebusContext, "Timeouts",true))
                
                .Routing(r => r.TypeBased()
                .MapAssemblyOf<Crm.Messages.Events.CustomerCreated>(settings.Settings.EndpointQueueName)
                .MapFallback("RebusErrors"))

                );

       

    }

``

The Error Occurs in the Saga handlers below

``

    public async Task Handle(LegalInfoAcquiredInFirstSystem first)
    {
        Data.GotLegalInfoFromFirstSystem = true;

        await PossiblyPerformCompleteAction();
    }

    public async Task Handle(LegalInfoAcquiredInSecondSystem first)
    {
        Data.GotLegalInfoFromSecondSystem = true;

        await PossiblyPerformCompleteAction();
    }

    async Task PossiblyPerformCompleteAction()
    {
        if (Data.GotLegalInfoFromFirstSystem && Data.GotLegalInfoFromSecondSystem)
        {
            await bus.Publish(new CustomerIsLegallyOk { CrmCustomerId = Data.CrmCustomerId });

            MarkAsComplete();
        }
    }

``

What could be the possible source of this error. Thanks

1

1 Answers

1
votes

I finally solved the problem by adding s.EnforceExclusiveAccess(); to the Saga options.