Compensation in WF is the mechanism by which previously completed work can be undone or compensated (following the logic defined by the application) when a subsequent failure occurs. Transactions allow you to combine multiple operations into a single unit of work. Using a transaction gives your application the ability to abort (roll back) all changes executed from within the transaction if any errors occur during any part of the transaction process. However, using transactions may not be appropriate if the work is long running. For example, a travel planning application is implemented as a workflow. The steps of the workflow may consist of booking a flight, waiting for manager approval, and then paying for the flight. This process could take many days and it is not practical for the steps of booking and paying for the flight to participate in the same transaction. In this scenario, compensation could be used to undo the booking step of the workflow if there is a failure later in the processing.
When a workflow contains activities that execute logic that should be undone or compensated if the workflow fails to complete and transactions are not appropriate, then these activities can be placed in a CompensableActivity activity. A CompensableActivity activity has several child activity sections including Body and ConfirmationHandler. The Body activity defines the compensable unit of business logic that runs during normal workflow execution and the CompensationHandler activity defines the unit of logic to compensate the work executed by the Body. When the compensation is invoked, either explicitly by using a Compensate activity or implicitly by the workflow's default compensation logic, the activity specified as the CompensationHandler executes, compensating the action of the Body activities. If the CompensationHandler activity is not provided, then the default compensation logic runs by compensating all the CompensableActivity activities nested in the Body that executed successfully in the Body, in reverse order of completion. For more information about compensation, see Compensation Programming Model.