WCF Fault Contracts - Day 2

This article explains fault contracts in Windows Communication Foundation. It also explains the difference between faults & exceptions and provides sample code for implementing fault contracts.

Introduction

 
This article explains fault contracts in Windows Communication Foundation. It also explains the difference between faults & exceptions and provides sample code for implementing fault contracts.
 
I highly recommend reading WCF Introduction and Contracts - Day 1 before this article.
 

Fault Contracts

 
Windows Communication Foundation enables us to specify the fault behavior of our service.
 
For example
 
We create a service and this service is consumed by a client. Suppose this service throws an exception. But how can the client be notified of this exception? Because whenever an exception is thrown from a service, it cannot be sent to the client. But if you want to know the reason then a fault contract can help us. We can send information regarding this with the fault.
 

Faults Vs. Exceptions

 
The main thing in faults and exceptions is that "faults and exceptions are not the same thing".

An exception is a .Net mechanism. It is used when the program encounters an error. The .Net language allows us to throw, catch and ignore the exception. At some point they should be handled or the .Net runtime will terminate the thread in which the exception was thrown.

Faults refer to the SOAP fault mechanism to transfer error information from a service to a client. WCF provides the FaultException class.
 
Whenever a service throws a FaultException, WCF serializes the information sent to the client.
 

FaultException

 
A FaultException is used to send untyped fault data to the client.
 
FaultException<TDetail>
 
This generic version is used to send typed fault data to the client. TDetail represents the type parameter for the fault information. Now let's see the example of typed fault data.

Create one WCF Service Application. Add the following segment of code in the Interface. In this code we are using the FaultContract attribute. It is applied to the interface only. Suppose we want to return various types of faults then we need to set the FaultContract attribute multiple times.
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.     [OperationContract]  
  5.     [FaultContract(typeof(FaultInfo))]  
  6.     string Topup(string Operator, string MobileNumber, double Amount);  
  7. }  
  8. [DataContract()]  
  9. public class FaultInfo  
  10. {  
  11.     [DataMember()]  
  12.     public string Reason = null;  
  13. }  
If we choose to use a typed fault then we need to define a DataContract for the type that holds the error information. In our example we create a datacontract for the FaultInfo class and we create one data member i.e. reason to hold the error information.
 
Add the following lines of code in the .svc.cs file. In this method we check whether the amount is double or not. If the amount is double then we throw a fault exception.
  1. public string Topup(string Operator, string MobileNumber, double Amount)  
  2. {  
  3.  if (Amount == 0.0 d)  
  4.  {  
  5.   FaultInfo fi = new FaultInfo();  
  6.   fi.Reason = "Amount should not be in double.";  
  7.   throw new FaultException < FaultInfo > (fi, new FaultReason(fi.Reason));  
  8.  }  
  9.  return "Recharge Successful.";  
  10. }  
If the amount is double then we create an object of the "FaultInfo" class and set the information we want to send to the client in the datamember i.e. the Reason. Finally we throw a fault exception of the fault info type.
 

Conclusion

 
This article explains FaultException and the difference between faults and exceptions. It also explains the sample code to implement fault exceptions. You can download the code and check the application.