Pages

Monday, April 16, 2012

Fault Contract in WCF


Exceptions are technology specific. Hence they cannot cross service boundaries. Soap represent standard for errors.WCF can transfer error from service to client through faults. If the error is a fault Exception then all the details are transferred else only generic fault.
i.e. InternalServiceFault
we can use either [ServiceBehavior] or to enable Fault.
Example:-
<serviceBehaviors>
        <behavior>
         
          <serviceMetadata httpGetEnabled="true"/>
         
          <serviceDebug includeExceptionDetailInFaults="True"/>
        behavior>
      serviceBehaviors>

You can throw a Soap Fault using the FaultException class
You can specify the fault code and reason when you create it. All this details will be traveled to the client side.
Example:-
 Throw New FaultException(Of FaultExceptionClass)(New FaultExceptionClass)
At the client side we can use the try catch block to catch this faultException
Example:- Catch ex As FaultException(Of FaultService.FaultExceptionClass)
            Console.WriteLine("Fault value " + ex.Detail.m_Reason)
            Console.WriteLine("Fault value " + ex.Detail.m_InnerReason)
Example:-