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:
- FaultException: to send an untyped fault back to the caller.
- FaultException<T>: to send a typed fault data to the client. It is basically a Generic Type.
Syntax
[OperationContract]
[FaultContract(typeof(MyException))]
string getDetails(int value);
The Fault Contract sample demonstrates how to communicate error information from a service to a client. The sample is based with some additional code added to the servie to convert an internal exception to a fault.