This article explains how we can implement a custom tracking service. I have first gone with the theory part and if you read that part diligently, the code would be a breeze. The workflow is kept very simple so that we can focus on the main aim of the application which is to first understand the tracking service capabilities and then implement a custom tracking service.
Well, this is my first article on The Code Project, but I have tried my best to make the recipe as delicious as possible. Please give feedback if you feel that the article can be improved.
Let's start by understanding the tracking capabilities provided by the workflow. Every running workflow instance is created and maintained by an in-process engine referred to as the Workflow Runtime Engine. The execution of the workflow can be tracked using the workflow tracking infrastructure. When a workflow instance executes, it sends events and associated data to tracking services that are registered with the workflow runtime. We can use the out-of-box
SqlTrackingService or write a custom tracking service to intercept these events and use the data provided as required.
The runtime sends three types of events to the tracking service:
- Workflow events: Describe the life cycle of the workflow instance that include Created, Completed, etc.
- Activity events: Describe the life cycle of an individual activity instance that include Executing, Closed, etc.
- User events: When creating the business logic for an activity, the activity author might want to track some business- or workflow- specific data. This can be achieved by calling any of the overloaded
Activity.TrackData methods. The data tracked in this manner is sent to the tracking service as a User Tracking Event.
Now as a user, we might not be interested in all kinds of events. Thus we need a mechanism through which we can specify our requirements. Thus we create a Tracking profile and the workflow runtime uses the information in the tracking profile to determine what events and data must be sent. Now the final tracking happens through a tracking channel which contains the information about where the information is to be stored (database, file system, output window as in this case). The service sends the events via a tracking channel object, obtained via the
GetTrackingChannel method of the
TrackingService object. The tracking service calls the
Send method of the tracking channel to send only those specific events.
The following steps summarize the whole thing:
- The workflow runtime requests a tracking profile for this particular instance (
TryGetProfile method). The tracking service creates a tracking profile object and returns it back to the tracking runtime.
- The workflow runtime requests a tracking channel for this particular workflow instance (
GetTrackingChannel method) from the tracking service.
- The tracking service creates a tracking profile object that describes what events/data needs to be provided by the runtime to the tracking service for this instance.
- After the workflow starts, it triggers workflow events, activity events, and user events.
- The tracking runtime sends the events and data, which the service requested via the tracking profile, to the tracking channel for this instance. The runtime calls the
Send method of this tracking channel when it needs to send an event. The events are sent to the tracking service as they happen. By using a different tracking channel for each instance, no thread synchronization issues will arise, because each workflow always runs on a single thread, and that ensures that we will not have multiple
Send calls at the same time in a given tracking channel.
- The tracking channel can store this information in a tracking store (SQL database in the case of the SQL tracking service).
- The information in the tracking store can be queried at any time.
Using the Code
The application includes a simple workflow "
SayHiWorkFlow" which prints "
hello" to the console. The application writes the tracked data to the output window. The important classes are
MyTrackingService which implement the base classes
I have tried to do as much commenting as possible and just hope that the code is easily understandable. The below snippet shows a part of the
Send method implementation.
protected override void Send(TrackingRecord record)
ActivityTrackingRecord atr = record as ActivityTrackingRecord;
WorkflowTrackingRecord wtr = record as WorkflowTrackingRecord;
UserTrackingRecord utr = record as UserTrackingRecord;
Debug.WriteLine("Received an user tracking record");
To run the code, you would require the following key components available on the Microsoft site:
- Microsoft .NET Framework 3.0 Redistributable Package available here.
- Visual Studio 2005 Extensions for Windows Workflow Foundation (EN) 3.0 available here.
This article explained the fundamentals of tracking service along with an example. Hopefully you now understand the fundamentals of tracking service and how to use it in an application.