Pages

Men

rh

6/10/2012

What is Fault Contract?

Fault Contract:-
It enables you, to specify the fault behavior of service. That is when your service might issue faults and what type of information will be sent  with the fault when it does issue one.

Fault VS Exception:-
Exception: Exception , as referred, to here , are a .NET mechanism used to communicate problems encountered as a program execute.

Fault:-
It referred to the SOAP fault mechanism for transferring error or fault conditions from  to a consumer. The SOAP specification includes a definition for SOAP faults, providing structure for the contents of the message body when error occur.

WCF FaultException Class:-
WCF provides a FaultException class. That is standard mechanism between the two worlds of .NET exception and SOAP faults. That is when your service implementation throws a Fault Exception, the WCF plumbing thereafter take care of serializing that back to the consumer as a SOAP Faults.

This Class comes in two forms.

FaultException:- Used to send untyped fault data back to the consumer.
FaultException<TDetail>:- This is used to send typed fault data back to the clients. Where TDetails represents the type parameter for the detailed fault information to be serialized back to the consumer as a part of SOAP fault message.

Fault Contract Attribute:-
This is  available in System.ServiceModel namespace. It enables a service developer to declare  which faults a given service operation might issue if the things go wrong. Following  are the key pieces of information pertinent to working with the FaultContractAttribute.

  • The attributes can be applied to operations only.
  • The attribute is not inherited.
  • The attribute can be applied multiple times.
  • The attribute constructor takes a Type object used to reference  the .NET type of the  detail object.


Example :-


[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    [FaultContract(typeof(string)]
    double Divide(double numerator, double Denominator);
}
public class CalculatorService : ICalculatorService
{
   public double Divide(double numerator, double Denominator)
   {
      if (Denominator == 0.00)
      {
         string faultDetail = "You can not divide by Zero";
         throw new FaultException<string>(faultDetail);
      }
      return numerator/ denominator;
 
}









No comments :

Post a Comment