Search code examples
nservicebus

Nservice bus: Different sagas started with the same event


Is it that possible to start different sagas with the same started message and the same endpoint?

For example i want to handle a message "user-signed-in".

And i have a 2 different sagas which should start with the above message:

  1. Will wait until user signed out and will publish some event.
  2. The second will wait until he purchase some product and will publish event.

That sagas are completly diffrenet and have different class name & saga data class name.

And question is: will both sagas start when "user-signed-in" message come?


Solution

  • A single message received by an endpoint can start multiple sagas.

    You will need to mark both sagas as started by that message type. For example:

    public class UserSessionSaga : IAmStartedByMessages<UserSignedIn>...
    

    and

    public class UserPurchasesSaga : IAmStartedByMessages<UserSignedIn>...
    

    UserSessionSaga will be completed by an event (signing out) that is different from an event completing UserPurchasesSaga. For that, you'll have to specify what additional messages each saga can handle using IHandleMessages<T> and correlate all the messages each saga can handle. See documentation here for the syntax.