SOAP fault is a mechanism for transferring error or fault conditions from a service to a consumer. The SOAP specification includes a definition for SOAP faults, providing structure for the contents of the message body when errors occur. This makes it possible for all the different SOAP stacks to issue faults in a standard way.
FaultException
Used to send untyped fault data back to the consumer.
FaultException<TDetail>
A generic version used to send typed fault data back to the client, where TDetail represents the type parameter for the detailed fault information to be serialized back to the consumer as part of the SOAP fault message.
The FaultContractAttribute
The FaultContractAttribute, also defined in System.ServiceModel, enables a service developer to declare which faults a given service operation might issue if things go wrong. Following are the key pieces of information pertinent to working with the FaultContractAttribute:
The attribute can be applied to operations only.
The attribute is not inherited.
The attribute can be applied multiple times; for instance, if your service operation might return different types of faults, you would have a FaultContractAttribute
declaration for each type of fault.
The attribute’s constructor takes a Type object used to reference the .NET type of the Detail object, that is, the type of fault information details you want to bundle with your faults.
[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.0d) {
string faultDetail = "You cannot divide by zero";
throw new FaultException<string>(faultDetail);
}
return numerator / denominator;
}
}
*Please note that instead string class in this example you can define your own custom exception class.