Compensation Sample

Download sample

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:

Compensation sample output

Using the Sample

To use this sample, you must create the SQL persistence database.

To create the SQL persistence database

  1. 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
    

    Note

    When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended to use a single database for both persistence and tracking.

  2. In the SQL Server Query Analyzer workspace, select the database that you created in step 1 from the list of available databases.

  3. 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.

  4. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.

  5. 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.

  6. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.

To build the sample

  1. Download the sample by clicking Download Sample in this topic.

    This extracts the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. 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.

See Also

Reference

CompensatableTransactionScopeActivity
CompensationHandlerActivity
CodeActivity
FaultHandlerActivity
FaultHandlersActivity
IfElseActivity
IfElseBranchActivity
ThrowActivity

Other Resources

Using the CompensationHandler Activity
Adding Compensatable Activities and Compensation Handlers
Compensation Overview
Activities Samples
Windows Workflow Foundation Samples

© 2007 Microsoft Corporation. All rights reserved.