Windows Workflow Foundation
Transcript of Windows Workflow Foundation
Windows Workflow Foundation
Mario SánchezMarch, 2007
2
Contents● Introduction● Workflow Styles● Catalog of Workflow Activities● Execution Model of Workflow Instances● Workflow Supporting Services● Workflow-based Applications● References
3
Introduction● Windows Workflow Foundation is a development framework
that enables you to embed workflows in .NET Framework applications.
● Windows Workflow Foundation is not itself an executable application or program; instead it enables you to create your own workflow applications.
● You can write your workflows directly in code, in markup, or in a combination of both.
● You can implement your own custom workflow patterns through custom activities that can be reused across workflows.
● Windows Workflow Foundation enables model-driven workflow development, providing natural design visibility and hiding system-level concerns such as transactions, state management, and concurrency control.
4
Workflow Styles● Windows Workflow Foundation supports multiple workflow-
authoring styles, such as sequential, state machine, and data-driven.
– The sequential style is straightforward and useful for repetitive, predictable operations that are always the same.
– The state machine workflow style consists of a set of event-driven states.
– The data-driven style relies on data to determine whether or not certain activities are run based on a local data state.
5
Sequential Workflows● The sequential workflow style executes a set of contained
activities in order, one by one. You can add other composite activities to a sequential workflow to achieve:– parallelism (ParallelActivity)– event-driven parallelism (EventHandlingScopeActivity)– data-driven execution (ConditionedActivityGroup)– event-driven branching (ListenActivity)– familiar imperative control flow patterns such as conditional
branching (IfElseActivity) and iteration (WhileActivity, ReplicatorActivity).
● You can also utilize the extensibility of Windows Workflow Foundation to write custom composite activities that implement whatever specific control flow patterns your solutions require.
6
Sequential Workflows● A sequential workflow executes activities in a sequential
manner until the last activity finishes. Sequential workflows are not necessarily completely deterministic, even under normal operation. For example, you can use a ListenActivity activity or a ParallelActivity activity, and the exact sequence of events can vary in these cases.
7
Sequential Workflows
8
State Machine Workflows● In the state machine style of workflow authoring, the author
models the workflow as a state machine. ● The workflow itself consists of a set of states. One state is
denoted as a start state. ● Each state can receive a certain set of events. Based on an
event, a transition can be made to another state. ● The state machine workflow can have a final state. ● When a transition is made to the final state, the workflow is
completed.
9
State Machine Workflows● State machine-related activities
– EventDrivenActivity● Used for states that rely on an external event to start executing.-
– SetStateActivity ● Specifies a transition to a new state.
– StateActivity ● Represents a state in a state machine.
– StateInitializationActivity● Executes when a state is entered.
– StateFinalizationActivity ● Executes contained activities when leaving a StateActivity
activity.
10
State Machine Workflows
11
State Machine Workflows
12
Workflow Authoring Modes● Supported authoring modes for workflow implementation:
– Code-only● This is the default authoring mode for Windows Workflow
Foundation. It enables you to use C# or Visual Basic code to specify a workflow using the Windows Workflow Foundation API set. The workflow definition uses C# or Visual Basic code to declare the workflow structure.
– Code-separation● This mode enables you to define workflows by using workflow
markup and combining it with C# or Visual Basic code-behind implementations.
– No-code● This mode enables you to author a workflow by using workflow
markup. You can then compile the workflow with the Windows Workflow Foundation command-line workflow compiler, or you can load the workflow markup file into the workflow runtime engine through a host application.
13
Workflow Activities - Control● Control Flow Activities
– IfElseActivity / IfElseBranchActivity ● Tests a condition on each branch and performs activities on the
first branch for which the condition equals true.– WhileActivity
● Enables your workflow to loop until a condition is met.– ParallelActivity
● Lets you schedule two or more child SequenceActivity activity branches for processing at the same time.
– ReplicatorActivity ● Creates multiple instances of a single child activity.
– SequenceActivity ● Provides a simple way to link multiple activities together for
sequential execution.– SynchronizationScopeActivity
● Executes contained activities sequentially in a synchronized domain.
14
Workflow Activities - Control● Control Flow Activities
– ConditionedActivityGroup ● Executes child activities based on a condition that applies to the
ConditionedActivityGroup activity itself and based on conditions that apply separately to each child.
– ListenActivity● A composite activity that contains only EventDrivenActivity child
activities.
– SuspendActivity ● Suspends the operation of your workflow to enable intervention
in the event of some error condition that requires special attention.
– TerminateActivity ● Enables you to immediately end the operation of your workflow in
the event of an error condition.
15
Workflow Activities - Interaction● Interaction Activities
– CallExternalMethodActivity● Used with the HandleExternalEventActivity activity for input and
output communications with a local service. – CodeActivity
● Enables you to add Visual Basic or C# code to your workflow.– DelayActivity
● Enables you to build delays in your workflow based on a time-out interval.
– InvokeWorkflowActivity ● Enables your workflow to invoke another workflow.
– PolicyActivity ● Used to represent a collection of rules. A rule consists of
conditions and resulting actions.
16
Workflow Activities - Web-Services● Web-Services Related Activities
– InvokeWebServiceActivity ● Enables your workflow to invoke a Web service.
– WebServiceFaultActivity ● Lets you model the occurrence of a Web service fault.
– WebServiceInputActivity ● Receives data from a Web service.
– WebServiceOutputActivity ● Responds to a Web service request made to a workflow.
17
Workflow Activities - Events● Event Related Activities
– EventDrivenActivity ● Wraps one or more activities that are executed when a specified
event occurs.– EventHandlersActivity
● Provides a framework for associating events with an activity.– EventHandlingScopeActivity
● Executes its main child activity concurrently with an EventHandlersActivity activity.
– HandleExternalEventActivity ● Used together with the CallExternalMethodActivity activity for
input and output communications with a local service.
18
Workflow Activities – Exceptions● Exceptions and Transaction Related Activities
– CancellationHandlerActivity● Used to contain cleanup logic for a composite activity that is
canceled before all the composite activity's child activities are finished executing.
– CompensatableSequenceActivity ● Compensatable version of SequenceActivity.
– CompensatableTransactionScopeActivity ● Compensatable version of TransactionScopeActivity.
– CompensateActivity ● Enables you to call code to undo or to compensate for operations
already performed by the workflow when an error occurs.– CompensationHandlerActivity
● Wrapper for one or more activities that perform compensation for a completed TransactionScopeActivity activity.
19
Workflow Activities – Exceptions● Exceptions and Transaction Related Activities
– FaultHandlerActivity ● Used to handle an exception of a type that you specify.
– FaultHandlersActivity ● Represents a composite activity that has an ordered list of child
activities of type FaultHandlerActivity.– TransactionScopeActivity
● Provides a framework for transactions and exception handling.– ThrowActivity
● Enables you to capture business exceptions thrown as part of the process metadata for a workflow.
20
Workflow Activities - States● States Related Activities
– SetStateActivity ● Specifies a transition to a new state.
– StateActivity ● Represents a state in a state machine workflow.
– StateFinalizationActivity ● Used in a StateActivity activity as a container for child activities
executed when leaving the StateActivity activity. – StateInitializationActivity
● Used in a StateActivity activity as a container for child activities executed when entering the StateActivity activity.
21
Workflow Execution● The Windows Workflow Foundation runtime uses one thread
per workflow instance.– The Workflow Scheduling Services controls the execution of
the instances.● Activities have a life-cycle
– Transitions that change the current state generate events● Initialized● Executing● Canceling● Closed● Faulting● Compensating
22
Activity State Model● Responsibles of transitions
– Red line represents the workflow runtime engine is responsible.
– Solid yellow line represents the parent activity is responsible.
– Blue line represents the workflow runtime engine is responsible.
– Dashed yellow line represents the workflow runtime engine is responsible.
● An activity may only close when all children activities are either in their Closed or Initialized states.
23
ParallelActivity Execution● ParallelActivity with SequenceActivity branches
– The branch activities do not execute in a true concurrent manner. Only one activity of one branch executes at a time.
– Processing begins with the execution of one activity in one of the SequenceActivity branches. When that activity is completed, the next activity in sequence in another branch executes, and so on.
– Activity execution switches between the SequenceActivity branches, with one activity executing at a time until all the SequenceActivity branches finish executing.
– ParallelActivity does not guarantee the exact order of execution across the SequenceActivity branches.
– If a SequenceActivity branch contains an activity such as a blocked DelayActivity activity, the next activity in sequence in the next SequenceActivity branch is executed.
24
ReplicatorActivity● The ReplicatorActivity activity enables the creation of an
arbitrary number of instances of a single activity during run time.
● Can contain only one child activity, but the child can be a composite activity.
● By default, the ReplicatorActivity activity finishes when all child activities have finished. The UntilCondition property can be used to stop execution of the ReplicatorActivity.
● The ExecutionType can be set to Sequential or Parallel.
25
ParallelActivity Execution
26
Correlations● The Windows Workflow Foundation runtime engine uses
correlation to map an inbound message with a specific HandleExternalEventActivity activity in a workflow instance.
● The mapping to the instance is done when the workflow instance InstanceId is passed to the ExternalDataEventArgs constructor.
● A correlation is defined by using interface attributes. This correlation data is required to correlate an event activity for a workflow instance.
27
Workflow Services● Windows Workflow Scheduling Services
– Manage threading of workflow instances
● Windows Workflow CommitWorkBatch Services – Enable custom logic regarding the commitment of work batches
(also known as persistence points).● DefaultWorkflowCommitWorkBatchService● SharedConnectionWorkflowCommitWorkBatchService
● Windows Workflow Persistence Services– Persist workflow instances to a storage medium for later
retrieval
● Windows Workflow Tracking Services– Track the execution of workflow instances
28
Workflow Scheduling Service● ManualWorkflowSchedulerService
– Provides a threading service that enables the host application that creates a workflow instance to donate the Thread on which the workflow instance is run.
– Host applications can run a workflow instance on a single Thread (that is, in synchronous mode). It blocks the execution of the host application until the workflow instance becomes idle.
– The workflow instance can only be executed by using the RunWorkflow method of this service.
– Controls the number of threads spawned in an ASP.NET process by reusing the thread that made the ASP.NET Web request to run the workflow instance.
– At any time, the number of active threads in the workflow runtime equals the number of active Web requests in the ASP.NET process.
● DefaultWorkflowSchedulerService
29
DefaultWorkflowSchedulerService● Manages the threads to run
workflow instances in an asynchronous manner.– Workflows waiting to run
are stored in the internal queue of the DefaultWorkflowSchedulerService.
– When the DefaultWorkflowSchedulerService wants to start a workflow, a thread is acquired from a thread pool.
– The figure shows how the DefaultWorkflowSchedulerService executes workflows in an asynchronous manner.
30
Workflow Tracking Service● Enables the tracking of
workflow-related information in a consistent, reliable, and flexible manner.
● Instance Tracking● Created ● Completed● Idle● Suspended● Resumed● Persisted● Unloaded● Loaded● Exception● Terminated● Aborted● Changed● Started
● The tracking framework is designed to enable hosts to observe workflow instances during execution by capturing events that are raised during workflow execution.
● Activity Tracking● Initialized● Executing● Canceling● Closed● Compensating● Faulting
31
Workflow Persistence Service● When certain conditions occur while a workflow is running,
the workflow runtime engine uses a persistence service, if one is loaded in the runtime, to persist state information about the workflow instance.– When atomic transactions in TransactionScopeActivity
activities and CompensatableTransactionScopeActivity activities are completed.
– When a workflow instance becomes idle and the UnloadOnIdle flag is set to true for a WorkflowPersistenceService. This occurs, for example, when you use a DelayActivity activity.
– When the runtime host application calls WorkflowInstance.Unload or WorkflowInstance.TryUnload.
– When a workflow instance is terminated or finishes.– When a custom activity using the PersistOnCloseAttribute
attribute completes.– When a CompensatableSequenceActivity activity is
completed.
32
Workflow Persistence Service● If one of the conditions is met the runtime engine calls
methods that are supplied by the persistence service to save state information about the workflow instance.
● SqlWorkflowPersistenceService manages Timer information. Every time DelayActivity activity uses a persistence service like the SqlWorkflowPersistenceService, the time span information associated with the activity is persisted as part of the workflow state. Every time the workflow instance is evaluated by the workflow runtime, pending timer events are processed.
● SqlWorkflowPersistenceService
33
SqlWorkflowPersistenceService● Serializes activity state.● Persists information of
Timer activities.● Manages locks and blocked
instances.● Can be added to the
Workflow Runtime with a configuration file or programmatically.
CompletedScopeuidInstanceID
completedScopeID
state
modified
InstanceStateuidInstanceID
state
status
unlocked
blocked
info
modified
ownerID
ownedUntil
nextTimer
34
Workflow Applications● Host application's responsibilities
– Create one or more processes and one or more application domains.– Provide isolation mechanisms as needed.– Marshal calls between application domains as needed.– Start workflow instances.– Create custom and local services.
● Additionally, a host application might do the following: – Control the loading and unloading of workflows from memory.– Listen for specific events and communicate them to a user or
administrator.– Set time-outs and retries for each workflow.– Expose performance counters.– Write log information for debugging and diagnostics.– Provide custom service implementations.– Create localized services to meet language requirements of the
hosting application and user base.
35
References● MSDN Library
– http://msdn2.microsoft.com/en-us/library/ms735967.aspx– http://msdn2.microsoft.com/en-
us/netframework/aa663328.aspx – http://blogs.msdn.com/davegreen/archive/2005/10/20/483309.a
spx