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.
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;
}
}
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.
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 theTestCommandingSaga.cs
file, I have two classes - the Saga and the Command Handler. I expect theSagaEndingEvent
only once, but it happens twice.Thoughts?