Track using a saga
Because Routing Slips carry their state on the wire, it’s not possible to query the state of a currently executing routing slip. Use this pattern when your solution requires tracking the progress of a Routing Slip that’s in-flight.
Build a routing slip
Section titled “Build a routing slip”var builder = new RoutingSlipBuilder(NewId.NextGuid());
// add your activities as normalbuilder.AddActivity("DownloadImage", new Uri("queue:download-image_execute?bind=false"), new { ImageUri = new Uri("https://images.google.com/someImage.jpg") });builder.AddActivity("FilterImage", new Uri("queue:filter-image_execute?bind=false"));builder.AddVariable("WorkPath", @"\dfs\work");Create the saga state machine
Section titled “Create the saga state machine”public class MonitorState : SagaStateMachineInstance{ public Guid CorrelationId { get; set; } public string CurrentActivity { get; set; } public string CurrentState { get; set; }}
public class MonitorRoutingSlip : MassTransitStateMachine<MonitorState>{ public MonitorRoutingSlip() { InstanceState(x => x.CurrentState);
Event(() => RoutingSlipCompleted, x => x.CorrelateById(context => context.Message.TrackingNumber));
During(Initial, Active, When(RoutingSlipActivityCompleted) .Then(context => context.Saga.CurrentActivity = context.Message.ActivityName) .TransitionTo(Active), When(RoutingSlipCompleted) .Then(context => context.Saga.CurrentActivity = "Completed") .TransitionTo(Completed) ); }
public State Completed { get; private set; } public State Active { get; private set; }
public Event<RoutingSlipActivityCompleted> RoutingSlipActivityCompleted { get; private set; } public Event<RoutingSlipCompleted> RoutingSlipCompleted { get; private set; }}Add a subscription to the routing slip
Section titled “Add a subscription to the routing slip”builder.AddSubscription(new Uri("queue:monitor-state?bind=false"), RoutingSlipEvents.Completed | RoutingSlipEvents.ActivityCompleted);
var routingSlip = builder.Build();