Datasheet

Chapter 1: Work ow Programming Principles
7
method of an activity returns, the activity is in a state known as Initialized, which is different from
another state known as Executing, the state an activity enters when its Execute method is scheduled for
execution.
It is very important to understand when the Initialize and Execute methods of the activities making up a
workflow are invoked. The Initialize methods are invoked when the CreateWorkflow method is invoked
on the workflow run time. This method loads the workflow into memory and synchronously invokes the
Initialize methods of the activities making up the workflow. Because this is done synchronously, you can
rest assured that when the CreateWorkflow method returns, the Initialize methods of all activities have
returned and all activities are in Initialized state.
This shows one of the main differences between the Execute and Initialize methods of the activities
making up a workflow. The Initialize methods are all invoked in one shot before the workflow instance
begins its execution. The Execute method of an activity is invoked only when the logical program control
reaches the activity, which could be days, months, or years after the workflow instance has begun its
execution. In other words, in principle, there is an indefinite time lag between when the Initialize
method of an activity is invoked and when its Execute method is invoked.
The Initialize method of an activity is a good place to perform initialization that must be done once
in the lifetime of an activity, which could be days, months, or even years. It is also a good place to perform
one - time initialization that must be performed to avoid possible problems caused by the indefinite time
lag between the executions of the Initialize and Execute methods. Following is an example of such a
scenario.
As discussed earlier, an activity may need to suspend its execution for an indefinite period of time
waiting for external input. The execution logic of the activity, which is contained inside the Execute
method of the activity, must create what is known as a workflow queue , whereby an external entity
deposits the data that the activity needs to resume its execution. Creating the workflow queue inside the
Execute method could cause problems because of the indefinite time lag between when the workflow
instance starts and when the Execute method of the activity is invoked. This is because the external
entity may attempt to deposit the data long before the Execute method of the activity is invoked. To
tackle this problem, the Initialize method of the activity should create the required workflow queues to
enable the external entities to deposit the required data even before the activity actually starts it
execution.
An activity normally needs to use one or more local services to initialize itself. These local services are
registered with the workflow run time and are made available to an activity through an
IServiceProvider object that is passed into the Initialize method of the activity when this method is
invoked. The next chapter discusses the local services that SharePoint registers with the workflow run
time. The Initialize method of your custom activity can access these SharePoint local services through the
IServiceProvider object passed into it as its argument.
The workflow run time automatically registers some basic standard services that every activity needs.
One of these standard services is a service named WorkflowQueuingService. As the name suggests, the
WorkflowQueuingService service provides workflow queuing services such as creating, deleting, and
accessing workflow queues.
Your implementation of the Initialize method can use the IServiceProvider object passed into it as its
argument to access the WorkflowQueingService service and use this service to create the workflow
queues whereby external entities must deposit the data that the execution logic of your activity needs.
c01.indd 7c01.indd 7 8/25/08 4:02:53 PM8/25/08 4:02:53 PM