Compensation defines the execution of business logic that results from a business exception. It is solely within the scope of the workflow designer who is modeling a business process; the triggering of compensation is defined by the user rather than the runtime. The runtime provides the mechanism to which compensation is defined.
In this sample, the unit of work that is compensated is contained in a CompensatableTransactionScopeActivity activity. A CompensationHandlerActivity activity is then defined for this scope, to which the compensating work is defined. Because a scope can only be compensated if it is completed, a ThrowActivity activity throws an exception after the scope has finished executing. This exception is caught by an exception handler that contains a compensate activity. The compensate activity specifies the target scope that the runtime will execute next.
In this scenario, a purchase order is received and funds are withdrawn from the buyer's credit card. The workflow checks to see if the selected product is available. If it is available, the product is shipped. If the product is discontinued, a DiscontinuedProductException is thrown. The DiscontinuedProductException is caught by the workflow exception handler. The exception handler then triggers the compensation handler that is defined by the CompensateActivity that is contained in the handler. The compensation handler then refunds the funds back to the buyer.
The output of the sample appears as follows:
Using the Sample
To use this sample, you must create the SQL persistence database.
To create the SQL persistence database
In Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named WorkflowPersistenceStore by using the SQL query statement:
CREATE DATABASE SqlPersistenceService
In the SQL Server Query Analyzer workspace, select the database that you created in step 1 from the list of available databases.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistence_Schema.sql.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.
On the File menu, click Open and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistence_Logic.sql.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.
To build the sample
Download the sample by clicking Download Sample in this topic.
This extracts the sample project to your local hard disk.
Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.
Go to the source directory of the sample.
At the command prompt, type MSBUILD <Solution file name>.
To run the sample
- In the SDK Command Prompt window, run the .exe file in the Compensation\bin\debug folder (or the Compensation\bin folder for the VB version of the sample), which is located below the main folder for the sample.
© 2007 Microsoft Corporation. All rights reserved.