Callback Usage

Component: Callbacks
NuGet Package NServiceBus (5.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 bus = MvcApplication.Bus;
    var statusTask = bus.Send("Samples.Callbacks.Receiver", message)
        .Register<Status>()
        .ConfigureAwait(false);
    return View("SendEnumMessage", await statusTask);
}

Send and callback for an int

public async Task<ActionResult> SendIntMessage()
{
    var message = new IntMessage();
    var bus = MvcApplication.Bus;
    var responseTask = bus.Send("Samples.Callbacks.Receiver", message)
        .Register()
        .ConfigureAwait(false);
    return View("SendIntMessage", await responseTask);
}

Send and callback for an object

public async Task<ActionResult> SendObjectMessage()
{
    var message = new ObjectMessage();
    var bus = MvcApplication.Bus;
    var responseTask = bus.Send("Samples.Callbacks.Receiver", message)
        .Register(
            completion: completion =>
            {
                return (ObjectResponseMessage) completion.Messages[0];
            })
        .ConfigureAwait(false);
    return View("SendObjectMessage", await responseTask);
}

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();
bus.Send("Samples.Callbacks.Receiver", message)
    .Register<Status>(
        callback: status =>
        {
            Console.WriteLine($"Callback received with status:{status}");
        });

Send and callback for an int

var message = new IntMessage();
bus.Send("Samples.Callbacks.Receiver", message)
    .Register<int>(
        callback: response =>
        {
            Console.WriteLine($"Callback received with response:{response}");
        });

Send and callback for an object

var message = new ObjectMessage();
bus.Send("Samples.Callbacks.Receiver", message)
    .Register(
        callback: asyncResult =>
        {
            var localResult = (CompletionResult) asyncResult.AsyncState;
            var response = (ObjectResponseMessage) localResult.Messages[0];
            Console.WriteLine($"Callback received with response property value:{response.Property}");
        },
        state: null);

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>();
    IBus bus;

    public EnumMessageHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(EnumMessage message)
    {
        log.Info("Message received, Returning");
        bus.Return(Status.OK);
    }
}

Return an int

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

    public IntMessageHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(IntMessage message)
    {
        log.Info("Message received, Returning");
        bus.Return(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>();
    IBus bus;

    public ObjectMessageHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(ObjectMessage message)
    {
        log.Info("Message received, Returning");
        var response = new ObjectResponseMessage
        {
            Property = "PropertyValue"
        };
        bus.Reply(response);
    }
}

Last modified