Windows Workflow Foundation:向跟踪服务(TrackingService)传递数据
2011-03-09 15:44
579 查看
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-boxSqlTrackingServiceor 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.TrackDatamethods. 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
GetTrackingChannelmethod of the
TrackingServiceobject. The tracking service calls the
Sendmethod 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 (
TryGetProfilemethod). 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 (
GetTrackingChannelmethod) 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
Sendmethod 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
Sendcalls 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
MyTrackingChanneland
MyTrackingServicewhich implement the base classes
TrackingChanneland
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
Sendmethod 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.............
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:
Microsoft .NET Framework 3.0 Redistributable Package available here.
Visual Studio 2005 Extensions for Windows Workflow Foundation (EN) 3.0 available here.
实例源代码下载
相关文章推荐
- 跟我一起学Windows Workflow Foundation(3)-----使用If/Else活动,定制活动处理工作流,使用事件传递数据
- WF中的跟踪服务(5):SqlTrackingService 的数据维护
- 跟我一起学Windows Workflow Foundation(3)-----使用If/Else活动,定制活动处理工作流,使用事件传递数据
- 跟我一起学Windows Workflow Foundation(3)-----使用If/Else活动,定制活动处理工作流,使用事件传递数据
- Android服务之Service(四)--ASDL传递复杂数据对象
- C#制作Windows service 服务 实战 ip 地址的实时 扫描(网络跟踪的问题)
- 和我一起学Windows Workflow Foundation(2)-----让WF通过参数接收数据 [转]
- WF数据跟踪SqlTrackingService的问题
- WF中的跟踪服务(3):使用SqlTrackingService跟踪规则
- WF中的跟踪服务(2):使用SqlTrackingService
- WF中的跟踪服务(3):使用SqlTrackingService跟踪规则
- WF中的跟踪服务(2):使用SqlTrackingService
- android之Service介绍之四 如何传递复杂数据的AIDL服务
- 和我一起学Windows Workflow Foundation(2)-----让WF通过参数接收数据
- 和我一起学Windows Workflow Foundation(2)-----让WF通过参数接收数据
- 和我一起学Windows Workflow Foundation(2)-----让WF通过参数接收数据
- WF (Windows Workflow Foundation) 入门向导 – 范例程序下载
- Windows网络数据流跟踪技巧
- Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C#
- WF工作流与Web服务的相互调用 —— 以InvokeWebServiceActivity在Workflow工作流调用Web服务