Datasheet
Chapter 1: Workfl ow Programming Principles
13
If not, it first registers a method named WorkflowQueue_QueueItemAvailable as event
handler for the QueueItemAvailable event of the WorkflowQueue object and then returns
ActivityExecutionStatus.Executing to inform the workflow run time that it hasn ’ t completed its
execution because it needs to wait indefinitely for the external entity to deposit the data into the
workflow queue.
When the external entity finally deposits the data in the workflow queue, the WorkflowQueue_
QueueItemAvailable event handler is scheduled for execution. This means that a work item (a delegate
that wraps this event handler) is added to the WF scheduler ’ s work queue. When the WF scheduler
finally invokes the event handler, it passes an instance of the ActivityExecutionContext object into the
method as its first argument. Listing 1 - 5 presents the implementation of the WorkflowQueue_
QueueItemAvailable event handler.
Listing 1 - 5: The WorkflowQueue_QueueItem event handler
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System;
namespace Chapter1
{
public class CustomActivity : Activity
{
void WorkflowQueue_QueueItemAvailable(object sender,
QueueEventArgs e)
{
ActivityExecutionContext activityExecutionContext =
sender as ActivityExecutionContext;
WorkflowQueuingService workflowQueuingService =
activityExecutionContext.GetService < WorkflowQueuingService > ();
WorkflowQueue workflowQueue =
workflowQueuingService.GetWorkflowQueue(
e.QueueName);
object data = workflowQueue.Dequeue();
// Consume the data
activityExecutionContext.CloseActivity();
}
}
}
This method takes the following steps:
1. As just mentioned, the WF scheduler passes an ActivityExecutionContext object as the first
argument of this method when it invokes the method. Therefore, this method first casts its
first argument to the ActivityExecutionContext type:
ActivityExecutionContext activityExecutionContext =
sender as ActivityExecutionContext;
2. The method then invokes the GetService generic method on this ActivityExecutionContext
object to access the WorkflowQueuingService service:
WorkflowQueuingService workflowQueuingService =
activityExecutionContext.GetService < WorkflowQueuingService > ();
c01.indd 13c01.indd 13 8/25/08 4:02:55 PM8/25/08 4:02:55 PM