Tracking the current state in a state machine workflow

by Matt Milner 11. October 2006 16:11
I've seen several requests for tracking the current state in a state machine workflow.  There are several options including adding custom activities, using call external method, or using the WorkflowIdled event and the StateMachineWorkflowInstance to do this.  They all have their drawbacks however. 
  1. Custom Activities - in order to use this, you either have to subclass the state activity or write a simple activity and remember to add it to every state.  HUGE headache to manage and maintain in either case and the custom state comes with its own set of problems. 
  2. using CallExternalMethod has the same issues as the custom activity. Who wants to add an activity all the time just to track data. 
  3. Using the idled event can work, but then you have to use the StateMachineWorkflowInstance class and this may entail calling up your workflow from persistence just to query information from it. 
The ideal solution would give you the current state when it is entered and allow you to get that information without having to do anything specific in your workflow in order to track it.  I've created a sample that uses a custom tracking service to track the state changes in a state machine workflow. 
The tracking service is pretty simple, defining a single activity tracking location to track the executing state of the StateActivity.  The tracking channel just writes out the state name to the console.  In your use, you can customize the tracking channel to do whatever makes sense in your application (write to DB, raise an event, etc.). 
Find the sample here and leave any feedback as comments. 


Windows Workflow Foundation