Compartilhar via


ConsoleTrackingService Sample

Download sample

This sample demonstrates how to create a tracking service and write the contents of tracked data to the console.

Windows Workflow Foundation provides a tracking infrastructure that enables applications to track workflow state changes and data. Additionally, it gives you the flexibility to create the most appropriate tracking services for your business needs and use those tracking services in the workflow runtime.

To write a tracking service, you must implement a tracking channel and a tracking service. The tracking channel receives the tracking records that are sent by the runtime. The tracking service provides the runtime with tracking profiles based on specific parameters and conditions. It is also responsible for providing a tracking channel that receives the data that is sent by the runtime.

Note

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

Sample Overview

The sample is a workflow console application that contains one workflow and a simple tracking service. SampleWorkflow is a workflow that has a code handler, in which a UserTrackPoint is emitted.

The complete tracking service is implemented in ConsoleTrackingService.cs and has the following TrackingChannelSample and ConsoleTrackingService implementations:

  • TrackingChannelSample implements the Send method as follows:

    public override void Send(TrackingRecord record)
    {
        // Filter on record type.
        if (record is WorkflowTrackingRecord)
        {
            WriteWorkflowTrackingRecord((WorkflowTrackingRecord)record);
        }
        if (record is ActivityTrackingRecord)
        {
            WriteActivityTrackingRecord((ActivityTrackingRecord)record);
        }
        if (record is UserTrackingRecord)
        {
            WriteUserTrackingRecord((UserTrackingRecord)record);
        }
    }
    

Additionally, TrackingChannelSample implements the InstanceCompletedOrTerminated method by writing a message to the console. The InstanceCompletedOrTerminated method is called by the tracking runtime to indicate that the workflow instance finished running, regardless of the tracking profile associated with that instance.

  • ConsoleTrackingService implements the following method:

    public override TrackingChannel GetTrackingChannel(TrackingParameters parameters)
    {
        //Return a tracking channel to receive runtime events.
        return new TrackingChannelSample(parameters);
    }
    

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 ConsoleTrackingService\bin\debug folder (or the ConsoleTrackingService\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

Advanced Topics

The following topics are not implemented in the sample. They are pointers to more advanced workflow topics to explore.

Runtime and Tracking Services Communication

Runtime calls to the tracking services are synchronous. The workflow execution is blocked until you return from the tracking service methods.

Profile Updates

Windows Workflow Foundation gives you the flexibility to communicate profile updates or removal to the tracking runtime by deriving from IProfileNotification and implementing event handlers as follows:

public class ConsoleTrackingService : TrackingService, IProfileNotification

public event EventHandler<ProfileUpdatedEventArgs> ProfileUpdated;
public event EventHandler<ProfileRemovedEventArgs> ProfileRemoved;

ReloadProfiles

If you want to reload a profile for a specific workflow instance, you can call ReloadTrackingProfiles on the instance as follows:

WorkflowInstance wi = wr.StartWorkflow(typeof(SampleWorkflow));
wi.ReloadTrackingProfiles();

This causes the runtime to call the TryReloadProfile method on the registered TrackingService to get the new profile for that instance.

GetProfile Overloads

The workflow runtime calls the following method to ask for a profile for a specific workflowInstanceId. Implement this method if you support reloading instance-specific profiles.

GetProfile(Guid workflowInstanceId)

The workflow runtime calls the following method to ask for a version-specific profile. Implement this method if you support reloading version-specific profiles.

GetProfile(Type workflowType, Version profileVersionId)

See Also

Reference

ActivityTrackingRecord
WorkflowTrackingRecord
IProfileNotification

Other Resources

Tracking Samples
Simple Tracking Sample
Query Using SQLTrackingService Sample
Tracking Using User Track Points Sample
EventArgs Tracking Sample
Query Using SQLTrackingService 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.