Execute a routing slip
Using the routing slip executor
Section titled “Using the routing slip executor”The IRoutingSlipExecutor is registered as a scoped service in the container and can be resolved within any active container scope. To execute a routing slip,
use the Execute method on the IRoutingSlipExecutor.
The following example shows how to execute a routing slip from a service.
using System;using System.Threading.Tasks;using MassTransit;
public class EmailDeliveryService{ readonly IRoutingSlipExecutor _executor;
public EmailDeliveryService(IRoutingSlipExecutor executor) { _executor = executor; }
public async Task<Guid> DispatchEmail(string emailAddress, CancellationToken cancellationToken = default) { var trackingNumber = NewId.NextGuid();
var builder = new RoutingSlipBuilder(trackingNumber); builder.AddActivity("send-email", new Uri("queue:send-email_execute?bind=false"), new {To = emailAddress, Body = "Hello World!"}); var routingSlip = builder.Build();
await _executor.Execute(routingSlip, cancellationToken);
return trackingNumber; }}The service should be registered in the container using AddScoped<EmailDeliveryService> to ensure that the service has a valid container scope.
Using the bus
Section titled “Using the bus”The routing slip can also be executed using the bus. This is useful when the routing slip is created without an available container scope. However, creating a container scope is preferred whenever possible.
await bus.Execute(routingSlip);Once built, the routing slip is executed, which sends it to the first activity’s execute URI. To make it easy and to ensure that source information is included, an extension method on IBus is available, the usage of which is shown below.
Routing Slip Events
Section titled “Routing Slip Events”During routing slip execution, events are published when the routing slip completes or faults. Every event message includes the TrackingNumber as well as a Timestamp (in UTC, of course) indicating when the event occurred:
- RoutingSlipCompleted
- RoutingSlipFaulted
- RoutingSlipCompensationFailed
Additional events are published for each activity, including:
- RoutingSlipActivityCompleted
- RoutingSlipActivityFaulted
- RoutingSlipActivityCompensated
- RoutingSlipActivityCompensationFailed
By observing these events, an application can monitor and track the state of a routing slip. To maintain the current state, an Automatonymous state machine could be created. To maintain history, events could be stored in a database and then queried using the TrackingNumber of the routing slip.