Tracking the current state in a state machine workflow

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. 
 

Posted Oct 11 2006, 10:11 PM by matt-milner

Comments

Rags wrote re: Tracking the current state in a state machine workflow
on 02-05-2007 7:24 PM
Can you tell us how we can get the previous state name with the current implementation? Thanks.
Matt Milner wrote re: Tracking the current state in a state machine workflow
on 02-07-2007 1:41 PM
If you want the previous state, you can use two different approaches. One would be to use the tracking service I used here and simply have the TrackingChannel send the current state to the tracking service (put a method on the service and pass a reference to the service in the channel constructor). Then the service, instead of simply holding this single value, could keep a queue or list of some sort of the states executed. This would get you most of what you want (the service doesn't currently persist the data so you might need to add that feature, otherwise when your workflow gets persisted, you would lose all history).

The other option is to use the StateMachineWorkflowInstance class included in WF which has a notition of the history and can provide you a quick answer to this question. See the SDK and docs for examples on using this class.

Let me know if that doesn't answer your question.
Web Form wrote ASP.NET and Windows Workflows Foundation
on 05-15-2007 8:39 PM
Combining ASP.NET 2.0 and Windows Workflow Foundation (WF) will provide us with all the essential tools
Emilio wrote re: Tracking the current state in a state machine workflow
on 11-16-2007 12:45 AM
There were few comments in the source, it is fine that the Channel writes the current state to the console but that is utterly simplistic :(

What if one wants to access that information from the hosting application, a WinForm in particular? would one then use the tracking service instances' GetChannel? but then what would be the parameter to use in the GetChannel method?

Matt Milner wrote re: Tracking the current state in a state machine workflow
on 11-16-2007 12:18 PM
Emilio,
thanks for the feedback, and of course you are right that this is a simplistic solution. If you want the host to be able to access the information, one way would be to have the tracking channel call a method on the tracking service class which inserts or updates the current state for the given instance id in some dictionary. Since the the tracking service can be easily referenced from the host, it can simply call a method on the service to lookup the current state from the dictionary. In another sample on this blog [1] I use that mechanism to store tracked data in memory in the tracking service so it is available to the host.

[1] http://www.pluralsight.com/blogs/matt/archive/2006/11/25/42637.aspx
bonginkosi wrote re: Tracking the current state in a state machine workflow
on 03-28-2008 1:28 AM
I just want to know how to create a persistance service for state machine work flow.
Matt Milner wrote re: Tracking the current state in a state machine workflow
on 03-28-2008 6:27 AM
If you want to use persistence for ANY workflow type, you can use the SQLWorkflowPeristenceService [1]. If you are interested in building custom persistence services, then you might want to check out this project on CodePlex where they are creating services for different databases. [2]
Matt

[1]http://msdn2.microsoft.com/en-us/library/ms734764(vs.85).aspx
[2] http://www.codeplex.com/WFTools

Add a Comment

(required)  
(optional)
(required)  
Remember Me?