Datasheet
Chapter 1: Workfl ow Programming Principles
11
The Uninitialize method, which is a WF concept, is very different from the Dispose method, which is a
CLR concept. You mustn ’ t include one - time uninitialization logic of your activity in the Dispose method
because the Dispose method is invoked every time the CLR object that represents your activity is about
to be disposed of — that is, every time your activity suspends its execution and is serialized into the
durable storage. You must include one - time uninitialization logic of your activity in the Uninitialize
method because WF guarantees that this method is invoked only once during the lifetime of your
activity.
Activity Execution
As the name suggests, the Execute method is where the execution logic of your activity should go. Every
activity inherits this method from the Activity base class:
protected void ActivityExecutionStatus Execute(
ActivityExecutionContext executionContext)
When the logical program control reaches an activity or logical program statement, the Execute method
of the activity is automatically scheduled for execution. It is very important to realize that the Execute
method is not invoked immediately. Instead, it is scheduled for execution. When an execution method
such as Execute is scheduled for execution, a work item is added to the WF scheduler ’ s work queue.
This work item is basically a delegate that wraps the method. The WF scheduler uses a strict FIFO (First
in - First out) algorithm to run scheduled work items.
When the WF scheduler invokes a work item and consequently the method that it encapsulates, it passes
an instance of a class named ActivityExecutionContext into the method. The ActivityExecutionContext
class plays several important roles in Windows Workflow Foundation:
It represents the execution context within which the method must run. Recall from earlier that
the execution context of an activity is allocated on the heap because an activity cannot rely
on the thread on which it is running to maintain its execution context. When a method of an
activity is invoked, an object that represents its execution context is explicitly passed into it as an
argument. This is very different from method invocation in traditional programming models
such as C#, where the execution context is implicit and is maintained in the thread ’ s stack.
The ActivityExecutionContext class implements the IServiceProvider interface, which is the
standard interface that every .NET service provider implements. Your activity ’ s implementation
of the Execute method can use the ActivityExecutionContext object passed into it as its
argument to access the services that its execution logic needs. As you ’ ll see in the next chapter,
SharePoint registers several services with the workflow run time. The Execute method of your
custom activity can use the ActivityExecutionContext object to access these services.
The ActivityExecutionContext enables you to create subordinate activity execution contexts.
This is discussed later in this chapter.
The Execute method of activities that expect data from external entities must check whether the
workflow queues they created during their initialization phase (inside the Initialize method) contain the
data they need. If not, then they should return ActivityExecutionStatus.Executing and let go of the
thread on which they ’ re running. Listing 1 - 4 presents an example of an Execute method.
❑
❑
❑
c01.indd 11c01.indd 11 8/25/08 4:02:54 PM8/25/08 4:02:54 PM