Replying to a Message

Component: NServiceBus
NuGet NServiceBus (4.x)
Standard support for version 4.x of NServiceBus has expired. For more information see our Support Policy.

The simplest way to reply to a message is using Reply method:

public void Handle(RequestDataMessage message)
{
    // Create a response message
    var response = new DataResponseMessage
    {
        DataId = message.DataId,
        String = message.String
    };

    // Reply sends a new message to the return address on the message being handled.
    bus.Reply(response);

    // Reply is equivalent to the following code:
    bus.Send(bus.CurrentMessageContext.ReplyToAddress, bus.CurrentMessageContext.Id, response);
}

Only use a reply when implementing the Request-Response pattern (also called the Full Duplex pattern). In this pattern the originator of the message should expect a response message and have a handler for it. For examples and more information see the Full Duplex Sample and the article Callbacks.

When using the Publish-Subscribe pattern an endpoint handling an event shouldn't use Reply. This is because the publisher might not expect a reply and has no message handler for the replied message.

The reply address is controlled by the sender of the message replying to. See how to influence the reply behavior when sending messages.


Last modified