Fault Contract in WCF With Learning Video

Video of the article is HERE

Objective 

This article will give basic introduction on 
  1. How to handle Exception at service side?
  2. How to use Fault contract at Service side?
  3. How to handle Service Exception at client side?
Few points about Exception at Service
  1. Exception is technology specific. 
  2. Exception should not share beyond service boundary. 
  3. Since Exception is technology specific they cannot be propagated to client.
  4. Exception are of types 
    • CLR Exception 
    • Windows32 Exception
    • Runtime Exception at service 
    • C++ Exception 
  5. Exception are very much native to the technology in which service is made. 
  6. Exception must be converted from technology specific information to natural information that can be communicated to client.
1.gif

SOAP Fault 

2.gif
 
FaultException<T>
  1. Service should throw FaultException<T> instead of usual CLR exception.
  2. FaultException<T> is specialization of Fault Exception.
  3. Any client that programs against FaultException can handle exception thrown by FaultException<T>. 
  4. The type parameter T conveys the error detail. 
  5. T can be of any type like Exception, CLR Type or any type that can be serialized.
  6. T can be of type Data contract. 
  7. T is a generic parameter conveys the error details. 
About Fault Contract
  1. Any Exception service want to share beyond service boundary comes must be goes at the part of service contract. 
  2. Fault contract says what type of exception service can throw to client. 
  3. The type in Fault Contract must be the same as the T of FaultException<T>
  4. Fault contract is the contact between service and client about what exception can be thrown from service to client. 
  5. Fault contract is attributing of a method in the service. So only attributed method can throw the type of exception specified in Fault Contract. 
Working sample 

1. Create a simple WCF service with only one method. This method will have one output and two input parameters. 

 3.gif

2. Implement the service contract 

4.gif 

3. Press F5 to host the service in cassini server.  Copy the URL from here. 

4. Create a console client and add the service reference of the service we created. 

5. Handle the exception at client side 

5.gif 

6. Press F5 to get the exception information at client side. On running , you will get the exception message as below

6.gif
 
So, we are getting the proper error message at the client side. 

For your reference, source code is as below 

Service side

IService1.cs

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

namespace FaultHandling
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(DivideByZeroException))]
        double divide(int num1, int num2);
    }  
}

Service1

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

namespace FaultHandling
{
    public class Service1 : IService1
    {
        public double divide(int num1, int num2)
        {
            if (num2 == 0)
            {
                DivideByZeroException myException = new DivideByZeroException();
                throw new FaultException<DivideByZeroException>(myException, "NUMBER 2 is 0");
            }
            return num1 % num2;
        }
    }
}

Client side

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication3.ServiceReference1;
using System.ServiceModel;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
            try
            {
                double result = proxy.divide(30,0);
                Console.WriteLine(result);
            }
            catch (FaultException<DivideByZeroException> ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}

Thanks for reading. I hope this article was useful for you. Happy Coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all