This topic applies to Windows Workflow Foundation 4 (WF4).
This sample shows four common scenarios surrounding the use of CompensableActivity and confirmation. The sample runs four workflows to show confirmation. This sample is available in declarative and imperative versions.
Confirm a Compensable Activity
The first workflow demonstrates how to confirm a compensable activity and consists of a sequence of two CompensableActivity instances. After the completion of the second CompensableActivity a confirm activity confirms the first activity. When the workflow successfully completes, all CompensableActivity instances that have not been confirmed or compensated are automatically confirmed using the default order. Without the confirm, the second CompensableActivity would have been confirmed first.
The second scenario shows the affect on default confirmation when a CompensableActivity is explicitly compensated. The workflow consists of a sequence of two CompensableActivity activities, after the second completes the first is explicitly compensated. As a result when the workflow, completes only the second CompensableActivity is confirmed.
Controlling the Order of Confirmation for Nested CompensableActivity Activities
The third scenario shows how to control the order of confirmation for nested CompensableActivity. The scenario consists of a CompensableActivity as the root of the workflow. The body of the root CompensableActivity is a sequence of three CompensableActivity. The ConfirmationHandler for the root CompensableActivity is a sequence that specifies to confirm the first then the second nested CompensableActivity. When the workflow completes it confirms the root CompensableActivity, which then confirms the first, second and third nested CompensableActivity, in that order. As a result, the default confirmation order is essentially reversed. Because the third CompensableActivity was not explicitly confirmed as part of the root CompensableActivity by the ConfirmationHandler, it is confirmed according to the default order. However, because it is the only unconfirmed CompensableActivity this just means it is confirmed.
Scoping of Variables
The fourth scenario shows how the lifetime of variables defined for a CompensableActivity or one of their parents are still in scope when the CompensationHandler, ConfirmationHandler or CancellationHandler handlers are executed even if the activity has completed or the workflow has completed. The workflow consists of a sequence of two CompensableActivity. In the body of the first, the variable
mySum is set to the sum of 5 and 10 and the result printed. In the body of the second CompensableActivity, the sum is set to the sum of the existing sum and 7 and the result printed. A custom confirmation handler is defined for the first CompensableActivity, which prints the sum, subtracts 7 and prints the sum again. It is clear by inspecting the printed sums that the variable is in scope not only for the bodies of both CompensableActivity but the ConfirmationHandler as well.
To run the sample
Load the solution in Visual Studio 2010.
Run the ConfirmationSample.exe application.
Observe the following output:
Explicit confirmation: Start of workflow CompensableActivity1: Body CompensableActivity2: Body CompensableActivity1: Confirmation Handler End of workflow CompensableActivity2: Confirmation Handler Explicit compensation: Start of workflow CompensableActivity1: Body CompensableActivity2: Body CompensableActivity1: Compensation Handler End of workflow CompensableActivity2: Confirmation Handler Custom confirmation handler: Start of workflow CompensableActivity1: Body CompensableActivity2: Body CompensableActivity3: Body End of workflow CompensableActivity1: Confirmation Handler CompensableActivity2: Confirmation Handler CompensableActivity3: Confirmation Handler Variable access in a confirmation handler: Start of workflow CompensableActivity1: Body CompensableActivity1: The sum is: 15 CompensableActivity2: Body CompensableActivity2: Adding 7 to the sum CompensableActivity2: The sum is now: 22 End of workflow CompensableActivity2: Confirmation Handler CompensableActivity1: Confirmation Handler CompensableActivity2: The sum is: 22 CompensableActivity2: After subtracting 12 the sum is now: 10 Press ENTER to exit.</pre>
|The samples may already be installed on your computer. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.