Query Using SQLTrackingService Sample

Download sample

This sample illustrates the use of the SQL tracking service that is provided by Windows Workflow Foundation to track the execution of a workflow.

The workflow runtime raises certain events while it is executing a workflow. The tracking service can catch these events and store information about them in a SQL Server database.

The workflow runtime raises the following types of events:

  • Workflow instance-level events. These events are raised when a workflow instance enters into a particular state. For example, an event is raised when the workflow instance is initialized or when the workflow instance is executing.

  • Activity-level events. A workflow consists of activities, and each activity follows a life cycle. During its life cycle, the activity passes through multiple states. An event is raised when an activity reaches a particular state.

  • User-tracked events. A workflow author can create custom tracking events and add them to the code-beside of the workflow.

Data from the workflow data context can be emitted and given to the tracking service together with the events.

When you are tracking a workflow, you might not be interested in all events for all states and activities. You can create a tracking profile to specify the events that you are interested in.


The time value in tracking data is in Coordinated Universal Time (Greenwich Mean Time).

Creating the Custom Tracking Profile

The sample illustrates how to create a custom tracking profile. This profile is inserted into the SQL tracking database to track the specified events. The tracking profile is created by using the tracking profile object model.

The tracking profile object model provides a set of classes that help create the instance tracking point (WorkflowTrackPoint), activity tracking points (ActivityTrackPoint), and user track points (UserTrackPoint). Each tracking point also contains a set of matching locations (WorkflowTrackingLocation, ActivityTrackingLocation, and UserTrackingLocation). A matching location describes the event to be tracked (which is different based on whether it is an instance track point or an activity track point) and the activity (or workflow in case of an instance track point) to be tracked.

The data to be extracted can also be specified in the track point. In this example, no data extracts have been specified.

Use the following code to create the tracking profile:

TrackingProfile profile = new TrackingProfile();
ActivityTrackPoint trackPoint = new ActivityTrackPoint();
ActivityTrackingLocation trackingLocation = new ActivityTrackingLocation(typeof(Activity));
trackingLocation.MatchDerivedTypes = true;

foreach (ActivityExecutionStatus status in Enum.GetValues(typeof(ActivityExecutionStatus)))

profile.Version = new Version("");

WorkflowTrackPoint wTrackPoint = new WorkflowTrackPoint();
WorkflowTrackingLocation wLocation = new WorkflowTrackingLocation();

foreach (TrackingWorkflowEvent wEvent in Enum.GetValues(typeof(TrackingWorkflowEvent)))

wTrackPoint.MatchingLocation = wLocation;

TrackingProfileSerializer serializer = new TrackingProfileSerializer();
StringWriter writer = new StringWriter(new StringBuilder(),CultureInfo.InvariantCulture);
serializer.Serialize(writer, profile);
String trackingprofile = writer.ToString();

In the previous code example, the last four lines specify how the tracking profile can be serialized into an XML format; the tracking profile is then added to the database by using the UpdateTrackingProfile stored procedure.

After the tracking profile is serialized, it looks like the following:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>

<TrackingProfile xmlns="http://schemas.microsoft.com/winfx/2006/workflow/trackingprofile" version="">



























<Type>System.Workflow.ComponentModel.Activity, System.Workflow.ComponentModel, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Type>
















The tracking profile is created by using the tracking profile schema.

Inserting the Tracking Profile in the SQL Tracking Database

The tracking profile is inserted into the tracking profile database by using the UpdateTrackingProfile stored procedure. The stored procedure stores the profile for a particular workflow type. When the runtime requests the tracking service for a profile for the type, the tracking service returns this custom profile instead of the default profile.

Registering the Tracking Service with the Workflow Host

Use the following code to register a tracking service with the workflow runtime:

WorkflowRuntime wRuntime = new WorkflowRuntime();
wRuntime.AddService(new SqlTrackingService(connectionString));

The second line registers the SQL tracking service with the workflow runtime. The connection string is the connection string of the tracking database.

Retrieving the tracking information

Once the tracking information has been saved to the database, it can be retrieved using a SqlTrackingQuery object, using the TryGetWorkflow method.

Creating the Tracking Database

The SQL services that are installed by Windows Workflow Foundation use Microsoft SQL Server to store information. You can use Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE) for these tasks.

Windows Workflow Foundation Setup does not install the databases that are required by these services; however, Setup installs the SQL scripts for creating and configuring the databases for these services.

To create the SQL tracking database

  1. Using Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named Tracking by using the SQL query statement:



    When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended to use a single database for both persistence and tracking.

  2. In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.

  3. On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\Tracking_Schema.sql.

  4. Execute the query by clicking Execute or by pressing F5 to create the SQL Tracking Service tables.

  5. On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\Tracking_Logic.sql.

  6. Execute the query by clicking Execute or by pressing F5 to create the SQL Tracking Service stored procedures.

To build the sample

  1. Download the sample by clicking Download Sample.

    This extracts the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the QueryUsingSqlTrackingService\bin\debug folder (or the QueryUsingSqlTrackingService \bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also



Other Resources

Tracking Samples
Simple Tracking Sample
Query Using SQLTrackingService Sample
Tracking Using User Track Points Sample
EventArgs Tracking Sample
ConsoleTrackingService Sample
RuleActionTrackingEvent Sample
File Tracking Service and Query Sample
Using the Tracking Profile Object Model Sample
SQL Data Maintenance Sample
Windows Workflow Tracking Services

© 2007 Microsoft Corporation. All rights reserved.