Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / C#

Windows Workflow Foundation: Say Hello to Tracking Service !!

Rate me:
Please Sign up or sign in to vote.
3.19/5 (22 votes)
15 Jan 2009GPL34 min read 95.1K   589   34   22
This article explains the intricacies of implementing a custom tracking service in Windows Workflow Foundation.

Sample image

Introduction

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.

Tracking Overview

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:

  1. 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.
  2. The workflow runtime requests a tracking channel for this particular workflow instance (GetTrackingChannel method) from the tracking service.
  3. 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.
  4. After the workflow starts, it triggers workflow events, activity events, and user events.
  5. 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.
  6. The tracking channel can store this information in a tracking store (SQL database in the case of the SQL tracking service).
  7. 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 MyTrackingChannel and MyTrackingService which implement the base classes TrackingChannel and TrackingService.

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.

C#
 protected override void Send(TrackingRecord record)
 {
 // These are three different types of tracking records available.
 ActivityTrackingRecord atr = record as ActivityTrackingRecord;
 WorkflowTrackingRecord wtr = record as WorkflowTrackingRecord;
 UserTrackingRecord utr = record as UserTrackingRecord;

 Debug.WriteLine(record.EventDateTime.ToShortDateString());
 //Omitted some code here.............
   if(utr!= null)
     {
     Debug.WriteLine("Received an user tracking record");
     Debug.WriteLine(utr.UserData);
     }
}

To run the code, you would require the following key components available on the Microsoft site:

  1. Microsoft .NET Framework 3.0 Redistributable Package available here.
  2. Visual Studio 2005 Extensions for Windows Workflow Foundation (EN) 3.0 available here.

Conclusion

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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
India India
Akshay loves exploring new technologies. C# is his preferred mode of expression.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Prasyee29-Oct-12 1:25
Prasyee29-Oct-12 1:25 
GeneralMy vote of 5 Pin
lixinsheng0078-Mar-11 20:35
lixinsheng0078-Mar-11 20:35 
GeneralMy vote of 1 Pin
Michael Ulmann18-Jan-11 19:08
Michael Ulmann18-Jan-11 19:08 
GeneralGood article Pin
shashankkadge24-Mar-09 8:25
shashankkadge24-Mar-09 8:25 
GeneralRe: Good article Pin
akshayswaroop26-Mar-09 17:54
akshayswaroop26-Mar-09 17:54 
GeneralMy vote of 1 Pin
Michael Freidgeim15-Jan-09 0:52
Michael Freidgeim15-Jan-09 0:52 
GeneralRe: My vote of 1 Pin
akshayswaroop15-Jan-09 16:00
akshayswaroop15-Jan-09 16:00 
GeneralRe: My vote of 1 Pin
NikoTanghe15-Jan-09 23:12
NikoTanghe15-Jan-09 23:12 
GeneralMSDN SDK ConsoleTrackingService Sample Pin
Michael Freidgeim14-Jan-09 2:55
Michael Freidgeim14-Jan-09 2:55 
Is your class better than MSDN SDK ConsoleTrackingService Sample[^]?
What are the benefits of using your class?

Michael Freidgeim.
Blog: http://geekswithblogs.net/mnf/

GeneralRe: MSDN SDK ConsoleTrackingService Sample Pin
akshayswaroop15-Jan-09 15:56
akshayswaroop15-Jan-09 15:56 
QuestionAdding new CustomTrackingService to existing persited workflows? Pin
DamnDante13-Apr-08 14:59
DamnDante13-Apr-08 14:59 
GeneralI want to making profilw.xml Pin
yesjang28-Jun-07 18:51
yesjang28-Jun-07 18:51 
GeneralRe: I want to making profilw.xml Pin
akshayswaroop4-Jul-07 22:27
akshayswaroop4-Jul-07 22:27 
QuestionTypical scenarios for custom tracking service Pin
P.R.Karve11-Feb-07 22:27
P.R.Karve11-Feb-07 22:27 
AnswerRe: Typical scenarios for custom tracking service Pin
akshayswaroop11-Feb-07 22:36
akshayswaroop11-Feb-07 22:36 
AnswerRe: Typical scenarios for custom tracking service Pin
Robert te Kaat22-Feb-07 8:20
Robert te Kaat22-Feb-07 8:20 
GeneralRe: Typical scenarios for custom tracking service Pin
akshayswaroop23-Feb-07 6:26
akshayswaroop23-Feb-07 6:26 
GeneralMSDN Link for Workflow Pin
akshayupta7-Feb-07 0:12
akshayupta7-Feb-07 0:12 
GeneralRe: MSDN Link for Workflow Pin
akshayswaroop7-Feb-07 0:22
akshayswaroop7-Feb-07 0:22 
GeneralRe: MSDN Link for Workflow Pin
akshayupta7-Feb-07 14:33
akshayupta7-Feb-07 14:33 
GeneralRe: MSDN Link for Workflow Pin
akshayswaroop7-Feb-07 18:56
akshayswaroop7-Feb-07 18:56 
GeneralRe: MSDN Link for Workflow Pin
Michael Freidgeim14-Jan-09 3:12
Michael Freidgeim14-Jan-09 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.