Callback Usage

Component: Callbacks
NuGet Package NServiceBus.Callbacks (1.x)
Target NServiceBus Version: 6.x
  1. Run the solution. Two console applications and a web application will start.
  2. In the WebSender application, click links from the landing page to trigger each scenario.
  3. In the Sender console application, when prompted to press various keys to trigger each scenario.

Shared Project

A class library containing the messages and shared code.

Status Enum

To be used for the enum callback scenario.

public enum Status
{
    OK,
    Error
}

WebSender project

An ASP.NET MVC application responsible for sending messages and handling the web callback from the reply. Depending upon NServiceBus version, the method for integrating with asynchronous controllers is very different.

All the usages of the Bus are done via a static instance stored on the root MvcApplication that is configured in the Global.cs.

Send and callback for an enum

public async Task<ActionResult> SendEnumMessage()
{
    var message = new EnumMessage();
    var sendOptions = new SendOptions();
    sendOptions.SetDestination("Samples.Callbacks.Receiver");
    var endpointInstance = MvcApplication.EndpointInstance;
    var statusTask = endpointInstance.Request<Status>(message, sendOptions);
    return View("SendEnumMessage", await statusTask.ConfigureAwait(false));
}

Send and callback for an int

public async Task<ActionResult> SendIntMessage()
{
    var message = new IntMessage();
    var sendOptions = new SendOptions();
    sendOptions.SetDestination("Samples.Callbacks.Receiver");
    var endpointInstance = MvcApplication.EndpointInstance;
    var statusTask = endpointInstance.Request<int>(message, sendOptions);
    return View("SendIntMessage", await statusTask.ConfigureAwait(false));
}

Send and callback for an object

public async Task<ActionResult> SendObjectMessage()
{
    var message = new ObjectMessage();
    var sendOptions = new SendOptions();
    sendOptions.SetDestination("Samples.Callbacks.Receiver");
    var endpointInstance = MvcApplication.EndpointInstance;
    var responseTask = endpointInstance
        .Request<ObjectResponseMessage>(message, sendOptions);
    return View("SendObjectMessage", await responseTask.ConfigureAwait(false));
}

Sender project

A console application responsible for sending a messages and handling the callback from the reply, as an alternative to a web application.

Send and callback for an enum

var message = new EnumMessage();
var sendOptions = new SendOptions();
sendOptions.SetDestination("Samples.Callbacks.Receiver");
var status = await endpointInstance.Request<Status>(message, sendOptions)
    .ConfigureAwait(false);
Console.WriteLine($"Callback received with status:{status}");

Send and callback for an int

var message = new IntMessage();
var sendOptions = new SendOptions();
sendOptions.SetDestination("Samples.Callbacks.Receiver");
var response = await endpointInstance.Request<int>(message, sendOptions)
    .ConfigureAwait(false);
Console.WriteLine($"Callback received with response:{response}");

Send and callback for an object

var message = new ObjectMessage();
var sendOptions = new SendOptions();
sendOptions.SetDestination("Samples.Callbacks.Receiver");
var response = await endpointInstance.Request<ObjectResponseMessage>(message, sendOptions)
    .ConfigureAwait(false);
Console.WriteLine($"Callback received with response property value:{response.Property}");

Receiver project

A console application responsible replying to messages from either the web application or the console application.

Return an enum

public class EnumMessageHandler :
    IHandleMessages<EnumMessage>
{
    static ILog log = LogManager.GetLogger<EnumMessageHandler>();

    public Task Handle(EnumMessage message, IMessageHandlerContext context)
    {
        log.Info("Message received, Returning");
        return context.Reply(Status.OK);
    }
}

Return an int

public class IntMessageHandler :
    IHandleMessages<IntMessage>
{
    static ILog log = LogManager.GetLogger<IntMessage>();

    public Task Handle(IntMessage message, IMessageHandlerContext context)
    {
        log.Info("Message received, Returning");
        return context.Reply(10);
    }
}

Return an Object

Note that this scenario requires a Reply with a real message.

public class ObjectMessageHandler :
    IHandleMessages<ObjectMessage>
{
    static ILog log = LogManager.GetLogger<ObjectMessageHandler>();

    public Task Handle(ObjectMessage message, IMessageHandlerContext context)
    {
        log.Info("Message received, Returning");
        var objectResponseMessage = new ObjectResponseMessage
        {
            Property = "PropertyValue"
        };
        return context.Reply(objectResponseMessage);
    }
}

Last modified