Running the sample
- Run the solution. Two console applications and a web application will start.
- In the WebSender application, click links from the landing page to trigger each scenario.
- 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
}
Callbacks should only be used in exceptional situations, for example, to introduce messaging behind a synchronous API in a legacy component that can't be changed. They allow gradually transitioning applications towards messaging. See the when to use callbacks section for more information.
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);
}
}