Follow by Email

Tuesday, June 4, 2013

Fault contract in WCF | Example of using fault contract in WCF asp.net | Handle Exceptions in WCF | Custom Error handling in WCF

Hi in this post i will show how to use fault contract in WCF to handle exceptions and how we can display a custom error message to client for the exception has occurred.

Taking a scenario where we have a divide by zero exception getting occurred . To that exception we can show a custom error message to the client as "Application Error has occurred. Please try again later" or we can also show the actual error generated by the .net application. To handle such an scenario we use Fault Contract.

Sample Code of WCF Service using Fault Contract to handle Exceptions:

1. Service Interface i.e IService2.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

[ServiceContract]
public interface IService2
{
    [OperationContract]
    [FaultContract(typeof(HandleException))]
    int GetData(int value, int value2);
}

[DataContract]
public class HandleException
{
    [DataMember]
    public string CustomExceptionMessage { get; set; }
    [DataMember]
    public string ErrorDesc { get; set; }
}


2. Consuming Interface into WCF .svc page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class Service2 : IService2
{
    public int GetData(int value, int value2)
    {
        HandleException Obj = new HandleException();
        try
        {
            return (value / value2);
        }
        catch (Exception ex)
        {
            Obj.CustomExceptionMessage = "An Application Error has occurred";
            Obj.ErrorDesc = ex.ToString();
            throw new FaultException<HandleException>(Obj, ex.ToString());
        }
    }
}


3. Consuming Service in my Client Application :
Here for the exception occurred in the service i will be showing the custom message in my client application which i have handled in the WCF Service catch block for every exception caused.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.ServiceModel;

public partial class FaultExceptionClient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ServiceReference1.Service2Client s2 = new ServiceReference1.Service2Client();
            int Z = s2.GetData(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
            Response.Write("<script language='javascript'>alert('Result: " + Z + "');</script>");
        }
        catch (FaultException<ServiceReference1.HandleException> ex1)
        {
            lblErrorMessage.Visible = true;
            lblErrorMessage.Text = ex1.Detail.CustomExceptionMessage + "<br/>" + ex1.Detail.ErrorDesc;    
        }
    }
}

Result: