Click here to Skip to main content
15,881,803 members
Articles / General Programming / Exceptions
Article

Fault Contracts in WCF

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 8.1K   1  
WCF provides us a facility to specify the fault behavior of our service. It provides a FaultException Class. Whenever our service implementation

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

WCF provides us a facility to specify the fault behavior of our service. It provides a FaultException Class. Whenever our service implementation throws a fault exception, the WCF takes care of serializing that back to the consumer as a SOAP fault. WCF takes care of when our service might issue faults and what type of information will be sent with the fault.

 

[ServiceContract]

public interface IMyFirstService  {

[OperationConract]

[FaultContract( typeof MyAppFault)) ]

void MyMethod();

]


WCF FaultException Class comes in two forms:-

1.FaultException: Used to send untyped data back to the consumer.

2.FaultException<TDetail> : It is used to send typed fault data back  to the consumer. Here TDetail represents the type parameter for the detailed fault information to be serialized back to the consumer as a part of the SOAP fault message. Client catching the exception can access the detailed fault information by getting it from the exception object’s Detail property.


FaultContractAttribute property defined in the System.ServiceModel. It enables the service developer to declare which faults a given service might issue. These attributes can be applied to the operations only. It can be applied multiple times, Suppose if our service operation might return different types of faults, we would have a FaultContractAttribute Declaration for each type. 

It uses a Type object, used to reference the .NET type of the Details object. This is the type of fault information which we want to bundle with our faults.


A Simple example:

[ServiceContract()]

public interface ICalcService

{

    [OperationContract]

[FaultContract( typeof(string))]

double Divide(double numerator, double denominator)

        }

public class CalcService :  ICalcService 

{

public double Divide(double numerator, double denominator)

{

if(denominator ==0.0d)

{

string faultDetail= ”you can not divide it by Zero”l

throw new FaultException<string>(faultDetail);

}

return numerator/denominator;

}

}

 

This article was originally posted at http://wiki.asp.net/page.aspx/1727/fault-contracts-in-wcf

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --