Using Request/Reply in SAGA NserviceBus

1.1k Views Asked by At

I'm creating a SAGA in NServiceBus. This saga is handling some string which has to be transformed then validated and finally imported. These three actions are separate services. I want the actions as separate handlers in NServiceBus. I'm using the Request/Reply functionality in NServiceBus. Like this:

 Bus.Send<TransformRequest>("Backend", m =>
    {
       m.string = string;
       m.MessageId = messageId;
    })
    .Register(i =>
    {
       Console.WriteLine("transform finished")
    });

The transformationHandler is as follows.

 public void Handle(TransformRequest message)
    {
        var transformationResult = _transformationService.Transform(message.string);

        var response = new TransformResponse()
        {
            string= transformationResult,
            messageId = message.messageId,
        };

        Bus.Reply(response); 
    }

My question is as follows. When a request is sent to the transformationHandler. A message is sent to the messagequeue. Then hypothetical, the server crashes. The server reboots, the server picks up the TransformationRequest, does it work and wants to do a reply to the Saga, but how? The saga is not alive anymore and can't handle the .Register. How do I handle this problem?

Thank you.

1

There are 1 best solutions below

1
On BEST ANSWER

NServiceBus will find the saga instance using a header in the response message automatically for you.

As Sean mentions you need to skip .Register and add a handler for transform result in your saga. The reason is that callbacks using .Register is stored in memory and therefor won't survive a restart