0
votes

I've read that each message handler is wrapped in an "ambient transaction", and that database access is automatically enlisted in that transaction when possible. Does NServiceBus do anything else with that transaction? Specifically, I'm wondering if it can somehow cancel any messages that a handler sends/publishes in the case of an exception.

In the code below, does the bus Send the ArchiveMessage as soon as the Send method is called, or does it queue it up and only send it if the handler executes successfully?

public class BadHandler
{
    public IBus Bus { get; set; }

    public void Handle(MyMessage msg)
    {
        Bus.Send(new ArchiveMessage(msg.MessageId)); //does this message send?
        throw new Exception("Something terrible happened, maybe my database connection failed!");
    }
}
1

1 Answers

2
votes

I this case the message would not be sent. MyMessage will be retried the configured number of times and them moved to the designated error queue. You can have greater control over that process if you wish, you would need to create a custom FaultManager.