Exception Handling in WCF Application


In this learn series, we are explaining WCF applications. Previous chapters explained various important concepts of WCF applications. You can read the full chapter series here:
In this chapter, we will learn one very important concept of WCF applications; "How to handle exceptions in WCF applications". We know that an exception is a very common scenario in applications and it might occur anywhere and anytime. So, to develop a robust application, we need to prepare for the worst. In a normal native .NET application it's very easy to handle an exception because the entire code is in the same platform and a try-catch block is enough to handle them. We can catch the exception, log it, or ignore it or do our custom exception.
But when we talk about SOA, things change a little bit. Because we are aware of our service but we are not aware of the client. We don't know on which platform the client is running and what its nature is.
From a WCF service perspective; all the exceptions written in a WCF service can be consumed by a normal try-catch block in a traditional .NET application. In fact, they are a normal .NET exception. The major challenge for a service is to propagate the exception information to the user. The reason for this is that the user of the service will most probably be consuming the service in the form of a document and SOAP message format and secondly because the consumer of the service could be in some other technology and sending him a .NET specific exception doesn't make any sense.
In a service-based application, when the applications are talking in terms of documents, in other words, soap messages then the error and exception reporting from the service to the service consumer also is in the form of a SOAP element.
A WCF service provides a very nice abstraction over those in the form of a FaultException and the service is able to catch the exception using a FaiultException class. Even if we try with a normal Exception class in WCF, it will not work.

Try with a normal Exception class

In this example, we will try to implement a normal exception class within the WCF application. In this example, we are trying to divide a number by itself. When we pass 0 as a parameter it will through an exception.
  1. public class Service1 : IService1  
  2. {  
  3.     public string Devide(int value)  
  4.     {  
  5.         try  
  6.         {  
  7.            value  = value / value;  
  8.            return Convert.ToString(value);  
  9.         }  
  10.         catch (Exception ex)  
  11.         {  
  12.             throw new Exception("Value should be greater then 0");  
  13.         }  
  14.     }  
  15. }  
Here is the sample output. We are seeing that a normal Exception class is unable to throw a new exception to the client. 

The solution
The solution is to throw an object of the FaultException class. In the following example, we will implement one full working exception example using the FaultException class. Try to use the following procedure.
Step 1: Create a WCF application
Create a WCF application and add the following code to it.
Code for Service contract.
This is small and easy code to implement a service contract. Don't forget to put a "[FaultContraact]" attribute over the Devide function. The FaultInfo function will be defined in the next step.
  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. namespace WCF  
  9. {  
  10.     [DataContract()]  
  11.     public class FaultInfo  
  12.     {  
  13.         [DataMember()]  
  14.         public string Reason = null;  
  15.     }  
  16.     public class Service1 : IService1  
  17.     {  
  18.         public string Devide(int value)  
  19.         {  
  20.             try  
  21.             {  
  22.                value  = value / value;  
  23.                return Convert.ToString(value);  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.                 FaultInfo fi = new FaultInfo();  
  28.                 fi.Reason = "Value should not be in 0";  
  29.                 throw new FaultException<FaultInfo>(fi, new FaultReason(fi.Reason));  
  30.             }  
  31.         }  
  32.     }  
  33. }  
Within the catch, we are creating an object of the FauException object and throwing it to the client. 
Step 2: Create a client application
This is a small console application that will consume the WCF service. We are sending 0 when we are trying to call the "Devide()" function and obviously it will throw an exception.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Diagnostics;  
  6. using System.Collections;  
  7. using Client;  
  8. using Client.ServiceReference1;  
  9. using System.ServiceModel;  
  10. namespace Client  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Client.ServiceReference1.Service1Client obj = new Service1Client();  
  17.             try  
  18.             {  
  19.                 string value = obj.Devide(0);  
  20.                 Console.WriteLine(value);  
  21.             }  
  22.             catch (FaultException ee)  
  23.             {  
  24.                 Console.WriteLine(ee.Message);  
  25.             }  
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29. }  
We are seeing that the client code is able to catch the exception. Here is the sample output.



In this example, we saw how to work with an exception in a WCF application. Hope you have understood the concepts. In the next chapter, we will look more into WCF services.
Next » Function Overloading in WCF Application