FaultHandlerActivity in Workflow


Introduction: A FaultHandlerActivity is same as Catch statement in C#. The FaultHandlerActivity has a FaultType property. This property represents the type of exception we want to catch. If we set the FaultType property to the System.Exception type, we will handle all CLS-compliant exceptions. When an exception is raised within a workflow and  if the exceptions is not handled, workflow terminates. When an exception is occurred  workflow check for any FaultHandlers form level to level of the workflow. Levels can be defined as several sequential workflows etc. If any level has handled the exception with the FaultHander workflow use that exception handler. otherwise goes for the termination of the  workflow. The FaultHandlerActivity can be used globally or it can be used for particular sequential workflow.  We can use many FaultHandlerActivities in the workflow and we should specify the exception that is going to handle.

Step 1 : Open Visual Studio 2010.

  • Go to File->New->Project.
  • Select Sequential Workflow Console Application.

fau8.gif

When we click the OK option the following design window opens.

fau1`.gif

Step 2 :
Now we go to Toolbox and drag activity.

  • Drag CodeActivity.

fau2.gif

Step 3 : Now double-click in CodeActivity and write the below code.

Code :

private
void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
       int i = 25;
       int ee = 5;
       int result = (i / ee);
}

Step 4 : To handle the exception we have to set the fault handlers. We can view the fault handlers by click in on the context menu in the sequential workflow.

fau4.gif

Step 5 :
We can select View Fault Handler and drag FaultHandlerActivity from Toolbox.

fau5.gif

Step 6 : We can set the type of the exception that the FaultHandlingActivity should handle. We can set it by the properties of the FaultHandlingActivity.

  • Right-click in FaulHandlerActivity.
  • Select Properties option.
  • Define  FaultType.

FaultType :

fau6.gif

Step 7 : Now again drag CodeActivity below the FaultHandlerActivity from Toolbox. Here in the codeActivity2 I have handled the exception by writing the exception message to the console.

fau7.gif

Step 8 : Double-click in CodeActivity and write the below code.

Code :

using
System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace WorkflowConsoleApplication4
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        public Workflow1()
        {
            InitializeComponent();
        }
        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            int i = 25;
            int ee = 5;
            int result = (i / ee);
        }
        private void codeActivity2_ExecuteCode(object sender, EventArgs e)
        {
            FaultHandlerActivity faultHandlerActivity = ((Activity)sender).Parent as FaultHandlerActivity;
            if (faultHandlerActivity != null)
            {
                Console.WriteLine(faultHandlerActivity.Fault.Message);
            }
            Console.ReadLine();
        }
    }
}

Step 9 : Now we go to Program.cs file and write the below code.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace WorkflowConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication4.Workflow1));
                instance.Start();
                waitHandle.WaitOne();
            }
        }
    }
}

Step 10 : Now we Press F5 and run the application.

fau10.gif


Similar Articles