Tuesday, 9 February 2016

Fault Contract - Handling Errors in WCF


Error Handling :

Whenever  we  writes a computer program we must curious about the Unexpected Exceptions.
An exception can  stops our entire program or may be it can cause performance degradation . So handling the exceptions are very important. In asp.net we have lot of exception handling mechanisms. But WCF is not that much straight forward.
The main problem is WCF is a service which is apart from our client so if any Exceptions are occurred in WCF code then we cannot able to find that through normal Exception handling in client side , if any error happens in our program then normally we need to propagate a user-friendly error message to the client, who consumes the WCF Service. For handling exceptions in WCF we are using Exception handling mechanism called Fault Contract
  
What is Fault Contract  ???

Fault Contract  is an exception handling mechanism which will help us to pass WCF Service errors/Exceptions to the client (Service consumer). In other words Fault Contract is mechanism to communicate error information between server and client
We can go through a simple example that demonstrate the use of Fault Contract .

Think in our WCF  Service Library we have a method like below
IService.cs

    [ServiceContract]
    public interface IService1
    {
       

        [OperationContract]
        int Divide(int Number1, int Number2);
    }
[DataContract]
    public class DivideClass
    {
        [DataMember]
        public  bool Result { get; set; }
        [DataMember]
        public  string ErrorMessage { get; set; }
        [DataMember]
        public  string ErrorDetails { get; set; }

    }
Service.cs
      public int Divide(int Number1,int Number2)
        {
            DivideClass objdiv = new DivideClass();
           
                int Result = Number1 / Number2;
                return Result;
           
        }
And we are try to use this service from a client like below
try
            {
                ExampleService.Service1Client obj = new ExampleService.Service1Client();
               
                int Result = obj.Divide(10, 0);
                            }
            catch (Exception e)
            {
           }
In the above code we are passing two variables 10,0 to the service and our service will divide the first number with second. Our second number is 0 so there should be a divided by zero exception. But we are using this service from a client so we don't have any idea about what is happening in WCF code, we don't have any information about the error and all. How can we get the error details from WCF??? for that purpose we are using Fault Contract.
See the below code
IService.cs

    [ServiceContract]
    public interface IService1
    {
       
        //Fault Contract Handling
        [OperationContract]
        [FaultContract(typeof(DivideClass))]
        int Divide(int Number1, int Number2);
    }

Service.cs
public int Divide(int Number1,int Number2)
        {
            DivideClass objdiv = new DivideClass();
            try
            {
                int Result = Number1 / Number2;
                return Result;
            }
            catch (Exception ex)
            {
                objdiv.Result = false;
                objdiv.ErrorMessage = "error occurred. Please try later.";
                objdiv.ErrorDetails = ex.ToString();
                throw new FaultException<DivideClass>(objdiv, ex.ToString());
            }
        }

In our Client Side
try
            {
               
                ExampleService.Service1Client obj = new ExampleService.Service1Client();
               
                int Result = obj.Divide(10, 0);
                            }
            catch (FaultException<ExampleService.DivideClass> Fex)
            {
                string ErrorDetails = Fex.Detail.ErrorMessage + "ErrorDetails::" +                Fex.Detail.ErrorDetails;
               
            }

Using the above method we can simply pass the error from WCF Service to our client (Service Consumer)



No comments:

Post a Comment