You are on page 1of 29

Visual Studio® 2008:

Windows® Workflow
Foundation
Module 6: Managing Faults, Cancellations,
Transactions, and Compensations
• Handling Faults

• Handling Cancellations

• Creating and Managing Transactions

• Creating and Handling Compensations


Lesson: Handling Faults
• Throwing Exceptions in a Workflow

• Overview of the Fault Handling Model

• Defining Workflow-Level Fault Handler Activities

• Defining Local Fault Handler Activities

• Demonstration: Handling Faults


Throwing Exceptions in a Workflow
A workflow can throw an exception to indicate an
exceptional condition in the workflow

To throw an exception programmatically:


• Create an exception object
• Throw the exception by using a Throw or throw statement

To throw an exception declaratively in a workflow:


• Add a ThrowActivity to the workflow
• Set the Fault or FaultType property
Overview of the Fault Handling Model
To handle exceptions in a workflow:
• Add a FaultHandlerActivity to your workflow
• Specify the exception type that the FaultHandlerActivity handles

The workflow runtime engine catches exceptions and


passes them to a FaultHandlerActivity asynchronously
• The runtime exits the scope where the exception occurred
• The runtime passes control to a FaultHandlerActivity, based on
the type of the exception
Defining Workflow-Level Fault Handler Activities
To define a workflow-level fault handler activity:
• Right-click the designer, and then click View Fault Handlers
• Add a FaultHandlerActivity to the FaultHandlers list
• Specify the FaultType property
• Add child activities to process the exception
Defining Local Fault Handler Activities
You can define local fault handlers for a composite activity
Demonstration: Handling Faults
In this demonstration, you will see how to:
• Throw an exception programmatically

• Throw an exception declaratively in a workflow

• Define a local fault handler

• Define a workflow-level fault handler


Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Handling Cancellations
• Overview of Cancellations

• Creating a Workflow That Requires Cancellation Handlers

• Handling Cancellations
Overview of Cancellations
What is cancellation?
• The workflow runtime engine cancels a composite activity
• Child activities in the composite activity are canceled before they
have finished executing

You can define a cancellation handler activity


• Invoked when the workflow cancels a composite activity
• Contains cleanup logic for the composite activity
Creating a Workflow That Requires
Cancellation Handlers
You typically use cancellation handlers for the following
activities:
• ListenActivity
• ConditionedActivityGroup

ListenActivity ConditionedActivityGroup
Handling Cancellations
You can handle cancellation at workflow-level or locally

Defining a
workflow-level
cancellation handler
Defining a local cancellation handler
for a composite activity
Lesson: Creating and Managing Transactions
• Overview of Transactions in Workflows

• Defining a Transaction Scope

• Configuring Transaction Options

• Demonstration: Creating and Managing Transactions


Overview of Transactions in Workflows
A transaction is an atomic unit of work
• Operations within the transaction succeed or fail atomically

Characteristics of a transaction:
• Atomicity
• Consistency
• Isolation
• Durability

Windows Workflow Foundation supports transactions


• Define a scope to wrap transactional operations
• If an error occurs in that scope, the workflow runtime engine
rolls back the transaction
Defining a Transaction Scope
To define a transaction scope:
• Add a TransactionScopeActivity to the workflow
• Enclose activities within the TransactionScopeActivity

Using TransactionScopeActivity:
• You cannot nest TransactionScopeActivity activities
• You must add the persistence service to the runtime engine
Configuring Transaction Options
Configure the following transaction options:
• IsolationLevel
• TimeoutDuration

Isolation levels
ReadUncommitted
ReadCommitted
RepeatableRead
Snapshot
Serializable
Chaos
Unspecified
Demonstration: Creating and Managing Transactions
In this demonstration, you will see how to:
• Enclose activities in a transaction scope

• Add the persistence service to support transactions

• Verify that the transactional activities succeed or fail


atomically
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Creating and Handling Compensations
• Overview of Compensation

• Compensating Non-Transactional Functionality

• Compensating Transactional Functionality

• Demonstration: Creating and Handling Compensations


Overview of Compensation
A workflow can model a long-running business process
• An error might occur after an earlier activity has completed
• Completed activities must be undone

Example of a workflow where compensation behavior is


required:
A customer purchases an item from a merchant

The merchant debits money from the customer's credit card

Later, the order is canceled because the item is discontinued

The merchant must credit the money back to the customer


Compensating Non-Transactional Functionality
To compensate non-transactional functionality:
• Add a CompensatableSequenceActivity to the workflow
• Add activities to the CompensatableSequenceActivity
• View the Compensation Handler in the designer
• Add compensation behavior to the Compensation Handler

Activities Compensation behavior


Compensating Transactional Functionality
To define a compensatable transaction scope:
• Add a CompensatableTransactionScopeActivity
• Add transactional activities to the scope
• View the Compensation Handler in the designer
• Add compensation behavior to the Compensation Handler

Transactional activities Compensation behavior


Demonstration: Performing Compensation
In this demonstration, you will see how to:
• Implement workflow activities that require compensation

• Enclose activities in a compensatable transaction scope

• Define compensation behavior

• Verify that the workflow performs compensation when


necessary
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lab: Managing Faults, Transactions, and
Compensations
• Exercise 1: Handling Faults

• Exercise 2: Implementing Transactional Behavior

• Exercise 3: Implementing Compensation Behavior


(If Time Permits)

Logon information

Virtual machine 6462A-LON-DEV-06

User name Student

Password Pa$$w0rd

Estimated time: 60 minutes


Lab Review
• Why does the CreateAppointmentWorkflow handle
communication exceptions locally, but handle database
exceptions at the workflow level?
• When should you use a TransactionScopeActivity?
When is CompensatableTransactionScopeActivity more
appropriate?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Best Practices
Course Evaluation

You might also like