Windows Workflow Foundation:向Tracing Service(TrackingService)傳遞資料

來源:互聯網
上載者:User

Source: http://msdn2.microsoft.com/en-us/library/bb264458(vs.80).aspx 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.

Collapse 1  protected override void Send(TrackingRecord record)
 2  {
 3  // These are three different types of tracking records available.
 4  ActivityTrackingRecord atr = record as ActivityTrackingRecord;
 5  WorkflowTrackingRecord wtr = record as WorkflowTrackingRecord;
 6  UserTrackingRecord utr = record as UserTrackingRecord;
 7 
 8  Debug.WriteLine(record.EventDateTime.ToShortDateString());
 9  //Omitted some code here.............
10    if(utr!= null)
11      {
12      Debug.WriteLine("Received an user tracking record");
13      Debug.WriteLine(utr.UserData);
14      }
15 }

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.

執行個體原始碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.