Command routing

Component: NServiceBus
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.

The sample demonstrates basic command routing between endpoints.

Running the project

  1. Start all the projects by hitting F5.
  2. In the Sender's console window send some orders by pressing S
  3. In the Sender's console window cancel some orders by pressing C
  4. Both messages are sent to the Receiver which logs a message

Code walk-through

Whenever the Sender sends a CancelOrder command it must specify the destination endpoint.

var command = new CancelOrder
{
    OrderId = orderId
};

await endpointInstance.Send("Samples.CommandRouting.Receiver", command)
    .ConfigureAwait(false);

Whenever the Sender sends a PlaceOrder command it does not need to specify a destination.

var command = new PlaceOrder
{
    OrderId = orderId,
    Value = value
};

await endpointInstance.Send(command)
    .ConfigureAwait(false);

This is enabled by configuring a message route for the PlaceOrder command in the endpoint configuration.

var transport = endpointConfiguration.UseTransport<LearningTransport>();
var routing = transport.Routing();

routing.RouteToEndpoint(
    messageType: typeof(PlaceOrder), 
    destination: "Samples.CommandRouting.Receiver"
);

Last modified