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 being replied 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 behaves when continuing the conversation (replying to a reply).
To request that replies to the reply go to this specific instance (not any other instance of the same endpoint):
var options = new ReplyOptions();
options.RouteReplyToThisInstance();
var myMessage = new MyMessage();
await context.Reply(myMessage, options);
To request that replies to the reply go to any instance of the endpoint (which overrides the public reply address setting):
var options = new ReplyOptions();
options.RouteReplyToAnyInstance();
var myMessage = new MyMessage();
await context.Reply(myMessage, options);
To request that replies to the reply are routed to a specific transport address:
var options = new ReplyOptions();
options.RouteReplyTo("MyDestination");
var myMessage = new MyMessage();
await context.Reply(myMessage, options);
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 roll back any transactions and use a custom recoverability policy to not retry non-transient errors.