Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring

Callback Usage

Component: Callbacks
NuGet Package: NServiceBus.Callbacks (3.x)
Target Version: NServiceBus 7.x

Running the sample

  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, press various keys to trigger each scenario when prompted.

Shared project

A class library containing the messages and shared code.

Status enum

This is used for the enum callback scenario.

public enum Status
{
    OK,
    Error
}

WebSender project

A .NET Core MVC application responsible for sending messages and handling the web callback from the reply. A message session is passed into the controller constructor.

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 status = await messageSession.Request<Status>(message, sendOptions);
    return View("SendEnumMessage", status);
}

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 status = await messageSession.Request<int>(message, sendOptions);
    return View("SendIntMessage", status);
}

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 status = await messageSession.Request<ObjectResponseMessage>(message, sendOptions);
    return View("SendObjectMessage", status);
}

Sender project

A console application responsible for sending 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);
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);
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);
Console.WriteLine($"Callback received with response property value:{response.Property}");

Receiver project

A console application responsible for 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);
    }
}