Getting Started

Replying to a Message

Component: NServiceBus
NuGet Package: NServiceBus (8.1)

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

public Task Handle(RequestDataMessage message, IMessageHandlerContext context)
    // 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.
    return context.Reply(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.

Influencing the reply behavior

A sender of a reply can influence how the requester will behave when continuing the conversation (replying to a reply). It can request a reply to go to itself (not any other instance of the same endpoint)

var options = new ReplyOptions();
var myMessage = new MyMessage();
await context.Reply(myMessage, options);

or explicitly to any instance of the endpoint (which overrides the public reply address setting)

var options = new ReplyOptions();
var myMessage = new MyMessage();
await context.Reply(myMessage, options);

It can also request the reply to be routed to a specified transport address instead

var options = new ReplyOptions();
var myMessage = new MyMessage();
await context.Reply(myMessage, options);