Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NServiceBus Send() vs SendLocal() and exceptions

Tags:

nservicebus

We are implementing a saga that calls out to other services with NServiceBus. I'm not quite clear about how NServiceBus deals with exceptions inside a saga.

Inside the saga we have a handler, and that handler calls an external service that should only be called once the original message handler completes succesfully. Is it okay to do:

public void Handle(IFooMessage message) 
{
    var message = Bus.CreateInstance<ExternalService.IBarMessage>();
    Bus.Send(message);

    // something bad happens here, exception is thrown
}

or will the message be sent to ExternalService multiple times? Someone here has suggested changing it to:

// handler in the saga
public void Handle(IFooMessage message) 
{
    // Do something
    var message = Bus.CreateInstance<ISendBarMessage>();
    Bus.SendLocal(message);

    // something bad happens, exception is thrown
}

// a service-level handler
public void Handle(ISendBarMessage message) 
{
    var message = Bus.CreateInstance<ExternalService.IBarMessage>();
    Bus.Send(message);
}

I've done an experiment and from what I can tell the first method seems fine, but I can't find any documentation other than http://docs.particular.net/nservicebus/errors/ which says:

When an exception bubbles through to the NServiceBus infrastructure, it rolls back the transaction on a transactional endpoint, causing the message to be returned to the queue, and any messages that user code tried to send or publish to be undone as well.

Any help to clarify this point would be much appreciated.

like image 301
elRobbo Avatar asked Sep 15 '25 12:09

elRobbo


1 Answers

As long as you're doing messaging from your saga and not doing any web service calls, then you're safe - no need to do SendLocal.

like image 197
Udi Dahan Avatar answered Sep 18 '25 12:09

Udi Dahan