Cancellation Handler on Compensable Activity

This sample demonstrates the use of a cancellation handler on a CompensableActivity.

This sample contains two scenarios that demonstrate the use of CompensableActivity cancellation.The first scenario contains a root compensable activity that contains three child compensable activities. Two child activities finish running their activity bodies successfully. When the third child activity body runs, it encounters an exception that is handled by canceling the third activity processing, after which the cancellation of the root activity is triggered. The logic in the root activity in this example is to compensate the other two child activities that completed earlier.

Try  
{  
    CA   
    {  
        CA1   
        {  
        }  
        CA2  
        {  
        }  
        CA3  
        {  
            //Exception here  
            // Then this will get cancelled  
        }  

       // Cancellation for the root activity automatically gets called, which, in turn, adds some logic to revert what was done (Or can decide to actually confirm CA1 & CA2 if the user so desires).  
    }  
}  
Catches {  
// Can do more stuff...  
}  

The second scenario demonstrates executing a TryCatch in parallel with a Delay, which finishes before the TryCatch branch. The completion condition is set to true once the first branch finishes, causing the other branch to be canceled.

Parallel   
{  
    Branch1   
    {  
        // Small Delay that times out (timeout1) before branch2.  
    }  
    Branch2   
    {  
        CA   
        {  
            CA1   
            {  
            }  
            CA2   
            {  
            }  
            CA3   
            {  
            }  
            If (timeout1)    
            {  
                call Cancel CA  
            }  
        }  
    }  
}  

To set up, build, and run the sample

  1. Using Visual Studio 2010, open CompensationCancellation.sln.

  2. Build the sample by pressing CTRL+SHIFT+B or select "Build Solution" from the Build menu..

  3. Run the sample by pressing F5 or select "Start Debugging" from the Debug menu. Alternately you may press Ctrl+F5 or select "Start without Debugging" from the Debug menu.

Important

The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

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.

<InstallDrive>:\WF_WCF_Samples\WF\Basic\Compensation\CompensationCancellation

See Also