Click here to Skip to main content
11,577,522 members (23,040 online)
Click here to Skip to main content

Fault Contracts in WCF

, 11 Oct 2013 CPOL 2.6K 1
Rate this:
Please Sign up or sign in to vote.
WCF provides us a facility to specify the fault behavior of our service. It provides a FaultException Class. Whenever our service implementation

Editorial Note

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;

}

}

 

License

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

Share

About the Author

ASP.NET Community
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.
Group type: Collaborative Group

583 members


You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid