Sending messages

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

NServiceBus supports sending different types of messages (see Messages, Events, and Commands) to any endpoint. Messages can be sent either directly from the endpoint or as part of handling an incoming message. When a message arrives at an endpoint, it goes through a pipeline of processing steps.

Outside a message handler

In some cases, messages that need to be sent may not be related to an incoming message. Some examples are:

  • Sending a command when a HTML form is submitted in an ASP.NET application.
  • Publishing an event when the user clicks a button on a GUI application (see Publish and Handle an Event).

To send a message directly from the endpoint:

Edit
var bus = Bus.Create(busConfiguration).Start();

var myMessage = new MyMessage();
bus.Send(myMessage);

Inside the incoming message processing pipeline

Messages often need to be sent as part of handling an incoming message. When running in a transaction mode that supports it, these send operations take part in the same transaction as that of the message handler, thereby ensuring that the send operations roll back if the handling of the message fails at any stage of the message processing pipeline.

To send a message from inside a message handler:

Edit
public class MyMessageHandler :
    IHandleMessages<MyMessage>
{
    IBus bus;

    public MyMessageHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(MyMessage message)
    {
        var otherMessage = new OtherMessage();
        bus.Send(otherMessage);
    }
}

The message can also be an interface rather than a concrete class:

Edit
bus.Send<IMyMessage>(
    messageConstructor: message =>
    {
        message.MyProperty = "Hello world";
    });

Overriding the default routing

The default routing for a message can be overridden by specifying a different destination address:

Edit
bus.Send(Address.Parse("MyDestination"), new MyMessage());

Sending to self

Sending a message to the same endpoint, i.e. Sending to self, can be done by sending a message to any of its own instances:

Edit
var myMessage = new MyMessage();
bus.SendLocal(myMessage);

Dispatching a message immediately

While it's usually best to let NServiceBus handle all exceptions, there are some scenarios where messages might need to be sent regardless of whether the message handler succeeds or not. For example, it might be desirable to send a reply notifying that there was a problem with processing the message.

This can be done by suppressing the ambient transaction:

Edit
using (new TransactionScope(TransactionScopeOption.Suppress))
{
    var myMessage = new MyMessage();
    bus.Send(myMessage);
}
Suppressing transaction scopes only works for MSMQ and SQL transports in DTC mode. Other transports or disabled DTC may result in unexpected behavior.

Related Articles

  • Message routing
    How NServiceBus routes messages between the endpoints.
  • Messages as Interfaces
    NServiceBus allows supports the use interfaces as well as standard XSD and class serialization.

Last modified