How to Implement Fault Contract in WCF

What is fault Contract?

 
A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client-side. The purpose of a Fault Contract is to handle an error by the service class and display in the client-side. Whenever the client makes a call to a service class and an unexpected exception comes such as, SQL Server is not responding, or Divide by zero; in this case, the service class throws an exception to the client using the Fault Contract.
 
By default when we throw an exception from a service, it will not reach the client. WCF provides the option to handle and convey the error message to the client from a service using a SOAP Fault Contract.
 
To support SOAP Faults, WCF provides the FaultException class. This class has two forms: 
  1. FaultException: to send an untyped fault back to the caller
  2. FaultException<T>: to send a typed fault data to the client. It is basically a Generic Type.
Syntax
  1. [OperationContract]  
  2. [FaultContract(typeof(MyException))]  
  3. string getDetails(int value);  
The Fault Contract sample demonstrates how to communicate error information from a service to a client. The sample is based on the, with some additional code added to the service to convert an internal exception to a fault.
 

What is the use of FaultContract?

 
In ASP.Net, C#, and VB.Net it's very simple to handle an exception just by adding the simple Try & Catch blocks. But when you talk about the WCF Service if an unexpected error occurs (like Server not responding properly and divide by zero) in a WCF Service then the exception details can be passed to the client using a Fault Contract.
 
FaultContract inherits from the FaultContractAttribute Class.
 
WCF.jpg 
 
Now we are going to implement the Fault Contract with database connectivity
 
Step 1: Database Setup
 
Create a table named EMPLOYEE with column names EMP_ID and EMP_NAME. We will create a service to input the user id and get the name of the employee. 
  1. SET ANSI_NULLS ON    
  2. GO    
  3. SET QUOTED_IDENTIFIER ON    
  4. GO    
  5. SET ANSI_PADDING ON    
  6. GO    
  7.     
  8. CREATE TABLE [DBO].[EMPLOYEE](    
  9.     
  10.           [EMP_ID] [INTNULL,    
  11.     
  12.           [EMP_NAME] [VARCHAR](50) NULL    
  13.     
  14. ON [PRIMARY]    
  15.     
  16. GO    
  17. SET ANSI_PADDING OFF    
  18. GO    
  19.     
  20. INSERT INTO EMPLOYEE(EMP_ID,EMP_NAME) VALUES(1,'AMIT')    
  21. INSERT INTO EMPLOYEE(EMP_ID,EMP_NAME) VALUES(2,'KIRTI')    
Step 2: Open Visual Studio then select "File" -> "New" -> "Project..." -> "WCF" -> "WCF Service Application".
 
1. Create IService.cs and Service.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. [ServiceContract]  
  9. public interface IService  
  10. {  
  11.     [OperationContract]  
  12.     [FaultContract(typeof(MyException))]  
  13.     string getDetails(int value);  
  14. }  
  15. [DataContract]  
  16. public class MyException  
  17. {  
  18.     private string strReason;  
  19.     public string Reason  
  20.     {  
  21.         get { return strReason; }  
  22.         set { Reason = value; }  
  23.     }  
  24. }
Service.cs
  1. public class Service: IService  
  2. {  
  3.  public string GetData(int value)  
  4.  {  
  5.   return string.Format("You entered: {0}", value);  
  6.  }  
  7.  string r = null;  
  8.  public string getDetails(int id)  
  9.  {  
  10.   try  
  11.   {  
  12.    SqlConnection con = new SqlConnection(@ "Data Source=AMIT-PC\MIND;Initial Catalog=wcf;Integrated Security=False;uid=sa;pwd=Mind1234");  
  13.    con.Open();  
  14.    SqlCommand cmd = new SqlCommand("SELECT EMP_NAME FROM EMPLOYEE WHERE EMP_ID= " + id + "", con);  
  15.    SqlDataReader rd = cmd.ExecuteReader();  
  16.    while (rd.Read())  
  17.    {  
  18.     if (rd == null)  
  19.     {  
  20.     } else  
  21.     {  
  22.      r = rd["EMP_NAME"].ToString();  
  23.     }  
  24.    }  
  25.    return r;  
  26.   } catch (Exception e)  
  27.   {  
  28.    MyException m = new MyException();  
  29.    m.Reason = "Some Problem";  
  30.    throw new FaultException < MyException > (m);  
  31.   }  
  32.  }  
  33. }


Similar Articles