Search code examples
c#nservicebussaga

Singleton NServiceBus Saga


What would be the correct way to have all the messages processed by a single saga?

I don't think I can not specify some message-to-saga correlation. Can I? I believe it would result in a "saga not found" error.

A naive way would be to have some constant ID in the saga, but that seems wrong.

class SomePolicy :
    Saga<SomePolicy.State>,
    IAmStartedByMessages<SomeEvent>
{
    internal class State : ContainSagaData
    {
        public int Id { get { return 1; } }
    }

    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<State> mapper)
    {
        mapper
            .ConfigureMapping<SomeEvent>(message => message.MagicConstant)
            .ToSaga(saga => saga.Id);
    }

    public void Handle(SomeEvent message)
    {
        // Modify the saga state here.
    }
}

Solution

  • Rather than override ConfigureHowToFindSaga you can supply an implementation of IFindSagas<T>.Using<M> which is used to find a saga of type T from a message of type M. Then just have it always return the same instance.

    See Complex Saga Finding Logic for more details and some samples.