daniellittledev / Enexure.MicroBus

MicroBus is a simple in process Mediator for .NET
MIT License
213 stars 26 forks source link

Saga duplicates task #29

Open dagilleland opened 6 years ago

dagilleland commented 6 years ago

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?

daniellittledev commented 6 years ago

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

dagilleland commented 6 years ago

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