Compensation Activity in Workflow


Introduction : The CompensationHandlerActivity is the container for activities that we wish to execute if compensation is necessary. This activity can only be to added to an activity that supports the ICompensatableActivity interface.

Let see how to create flow decision activity:

Step1 : Go to Visual Studio.

  • Select File->New->Project.
  • Select Workflow Console Application.
  • Workflow1.xaml file open.

    workflow.gif

Designing the Workflow.

Step : 2 Go to the Toolbox and drag an  activity.

  • Open Workflow1.xaml file in design view.
  • Drag the TryCatch activity.
  • Drag the Sequence activity to the Try section and set DisplayName prepration.
  • Click Add new catch in Catch section and select "Browse for types".
  • Select CallItoffException.

    try-catch.gif

Step : 3 Go to Program.cs and write the below code for a normal Workflow activity.

Code :

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Threading;
namespace Wo
{
    class Program
    {
        static void Main(string[] args)
        {
            AutoResetEvent syncEvent = new AutoResetEvent(false);
            WorkflowApplication i = new WorkflowApplication(new Workflow1())
             i.OnUnhandledException = (waueea) =>
                {
                   Console.WriteLine("{0}waueea.UnhandledException.GetType, waueea.UnhandledException.Message);
                    return UnhandledExceptionAction.Cancel;
                };
            i.Completed = (wacea) => { syncEvent.Set(); };
            i.Run();
            syncEvent.WaitOne();
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
        }
    }
   public class CallItOffException : Exception
   {
public CallItOffException(): base()
{
}
public CallItOffException(string message): base(message)
{
}
    }
}

Using a Compensable Activity:

Step 4 : Drag a Compensable activity from the Toolbox.

The Compensable activity consists of four parts.

  • Body.
  • CompensationHandler.
  • ConfirmationHandler.
  • CancellationHandler.

Designing the Activity:

Step 5 : Drag a Sequence activity onto the Body section.

  • Drag the WriteLine activity in Sequence.
  • Set the DisplayName tample.
  • Text property "Body : the tample".
  • Drag a Delay activity and set the Duration (TimeSpan.FromSeconds).

cancel1.gif

Step 6 : Press F5 to run the application.

result2.gif

Designing the Compensation Handlers:

Step 7 : Drag a Sequence activity to the Compensation Handlers.

  • Drag a WriteLine activity and change DisplayName and Text property.

compen1.gif

Step 8 : Press F5 to run the application.

result2.gif


Similar Articles