Replying to a Message

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

The simplest way to reply to a message is using the 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.

Replies participate in the handler transaction and are not sent if the message rolls back. If the code requires a response whether or not message processing succeeds, use immediate dispatch on the reply options. Make sure exceptions are rethrown to rollback any transactions and use a custom recoverability policy to not retry non-transient errors.

Last modified