Enexure.MicroBus icon indicating copy to clipboard operation
Enexure.MicroBus copied to clipboard

Saga duplicates task

Open dagilleland opened this issue 6 years ago • 2 comments

When using a Saga to kick off a command that will, in turn, generate an event to finish the saga, the generated event happens twice.

image

I'm not sure if that's because I'm "doing it wrong", or if there's a problem in the way the sagas are run.

I created a fork (see reference below) with a unit test (CompleteCommandingSagaTests.cs) to demonstrate the problem. Specifically, in the TestCommandingSaga.cs file, I have two classes - the Saga and the Command Handler. I expect the SagaEndingEvent only once, but it happens twice.

public class TestCommandingSaga : ISaga,
        ISagaStartedBy<SagaStartingAEvent>,
        IEventHandler<SagaEndingEvent>
    {
        public Guid Id { get; protected set; }
        public bool IsCompleted { get; protected set; }
        public static string Status { get; protected set; }

        private readonly IMicroBus Bus;
        public TestCommandingSaga(IMicroBus bus)
        {
            Bus = bus;
        }

        public async Task Handle(SagaStartingAEvent @event)
        {
            Id = @event.CorrelationId;
            Status = "Started, ";
            await Bus.SendAsync(new EndSaga { CorrelationId = Id });
        }

        public async Task Handle(SagaEndingEvent @event)
        {
            Status += "Finished.";
            IsCompleted = true;
        }
    }

    public class EndSagaCommandHandler : ICommandHandler<EndSaga>
    {
        private readonly IMicroBus Bus;
        public EndSagaCommandHandler(IMicroBus bus)
        {
            Bus = bus;
        }

        public Task Handle(EndSaga command)
        {
            Bus.PublishAsync(new SagaEndingEvent { CorrelationId = command.CorrelationId });
            return Task.CompletedTask;
        }
    }

Thoughts?

dagilleland avatar Sep 11 '18 07:09 dagilleland

Thanks for reporting the problem, I plan on looking into this.

daniellittledev avatar Oct 15 '18 00:10 daniellittledev

If you would like a hand, just let me know.

dagilleland avatar Oct 15 '18 12:10 dagilleland