Fault Exception Concept in WCF

What is a Fault Exception in WCF and why do we use it? This article explains the need for a Fault Exception.
 

Fault exception in WCF

 
It is used in a client application to catch contractually-specified SOAP faults. By the simple exception message, you can't identify the reason of the exception, that's why a Fault Exception is useful.
 
In this article I will:
  • Create a WCF service application that has an operation contract for the division of 2 passed values by the client.
  • Create a Console application and add a reference for the WCF service into it. Then access the method of the WCF service to do the division of passed values.
To understand it in details try the following example:
 
Step 1
  • Create a WCF Service Application named "FaultException".

    WCF Service Application

  • Write an operation contract in the interface of the default file named "IService1.cs".
    1. [OperationContract]  
    2. int Divison(int val1, int val2);
    operation contract

  • Write the following code in "Service1.svc.cs" for implementation of an operation contract for the division of 2 values with a try and catch block.
    1. public int Divison(int val1, int val2)  
    2. {  
    3.     try  
    4.     {  
    5.         return val1 / val2;  
    6.    }  
    7.    catch (Exception ex)  
    8.    {  
    9.         throw ex; // throw the exception if raises  
    10.    }  
    11. }
    Service1
     
  • Run the WCF service and note down the URL like "http://localhost:3857/Service1.svc" somewhere.

    Run the WCF service
Step 2
  • Create the console application named "ClientApp" for the client.

    console application

  • Right-click on the project and select "Add Service Reference".

    Add Service Reference

    Type the URL into the address TextBox that you have noted earlier then click on the Go button and click OK.

    Note: You can also change the namespace like here "ServiceReference1" is the default.

    ServiceReference1

    You can see the reference has been added in your application successfully.

    the reference has been added
     
  • Write the code that will accept the 2 integer values respectively at runtime from the user and then print the results after accessing the "Divison" method via an object of the WCF service.
    1. int val1 = Convert.ToInt32(Console.ReadLine());  
    2. int val2 = Convert.ToInt32(Console.ReadLine());  
    3. ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();  
    4. int result = myClient.Divison(val1, val2);  
    5. Console.WriteLine("Divison is = " + result);  
    6. Console.ReadKey();
    object of WCF service

    I have entered 2 integer values, 6 and 3; that's why the division will be 6/3=2.

    integer value

    But if I will enter 6 and 0 then It will generate the exception and print the exception due to the try-catch block.

    try-catch block
Question: How will the client know what the problem is that occured?
 
Solution: FaultException will tell the actual error so we need to write the FaultException in the WCF service named "Service1.svc.cs". The catch block will be like this:
  1. throw new System.ServiceModel.FaultException(ex.Message);  
And build the application.
 
build the application 
 
Now we need to update the reference of the WCF service in the client application
 
to update the reference of WCF service 
 
Run the client application with 6 and 0 values as done above and see the exact exception or problem.
 
exact exception or problem 


Similar Articles