Datasheet

Chapter 1: Work ow Programming Principles
12
Listing 1 - 4: An example implementation of the Execute method
Using System;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
namespace Chapter1
{
public class CustomActivity : Activity
{
protected override ActivityExecutionStatus Execute(
ActivityExecutionContext executionContext)
{
WorkflowQueuingService workflowQueuingService =
executionContext.GetService < WorkflowQueuingService > ();
WorkflowQueue workflowQueue =
workflowQueuingService.GetWorkflowQueue(
this.Name);
if (workflowQueue.Count > 0)
{
object data = workflowQueue.Dequeue();
// Consume the data here
return ActivityExecutionStatus.Closed;
}
workflowQueue.QueueItemAvailable +=
new EventHandler < QueueEventArgs > (
WorkflowQueue_QueueItemAvailable);
return ActivityExecutionStatus.Executing;
}
}
}
As shown in the preceding code, the Execute method takes these steps:
1. The ActivityExecutionContext class exposes a generic GetService method in addition to the
standard GetService method that returns a strongly typed service. Execute invokes this generic
method to access the WorkflowQueuingService service:
WorkflowQueuingService workflowQueuingService =
executionContext.GetService < WorkflowQueuingService > ();
2. Execute invokes the GetWorkflowQueue method on the WorkflowQueuingService service,
passing in the workflow queue name, to access the WorkflowQueue object that represents the
workflow queue that the activity created in its initialization phase inside its Initialize method:
WorkflowQueue workflowQueue =
workflowQueuingService.GetWorkflowQueue(this.Name);
3. Execute then checks whether the external entity has deposited the data in the workflow queue.
If so, it invokes the Dequeue method on the WorkflowQueue object to dequeue the data,
consumes the data, and returns ActivityExecutionStatus.Closed to inform the workflow run
time that it has completed its execution.
c01.indd 12c01.indd 12 8/25/08 4:02:54 PM8/25/08 4:02:54 PM