User Id :    Password :      New Member   Forgot Password
Exception handling in WCF using SOAP Fault
Description This article is all about exception handling in WCF Service using Fault Exception. Here I am describing how to throw and handle Fault Exception.   No. of Views     2021
  Rating     0
Author Sumit Gupta   Posted On     02 Aug 2011
Tags ASP.NET,C#,WCF    

Sample Code   Download Code

The WCF library provide the FaultException class. You can find it in the System.ServiceModel namespace. If a WCF service thorws a FaultException object, the WCF runtime generates a SOAP Fault message that is sent back to the client application.

Here is the code to send Fault Exception:


// put your statements here..

catch(Exception ex)
 throw new FaultException("Your Message comes here :" + ex.Message, new FaultCode("SenderName"));

So if an exception occurs, this code create a new FaultException object with the details of exception and thorw it back to the client application. Fault code provides a machine-readable identifier that can be used to programmatically identify the error condition and respond as appropriate. If you don't create a FaultCode object, the WCF runtime will automatically generate a FaultCode object.

Code to catch FaultException at Client application:


// call the WCF Service here..

catch(FaultException fe)
Response.Write(fe.Code.Name + ":" + fe.Reason);

So above function will display the fault code name and the Reason of exception that we have set at the time of creating FaultException object.

I hope it will help..


Happy Coding..



About Author

About Author I am cool and fun loving guy and enjoy my work. I love music and play table-tennis.... Sumit Gupta
No Photo
Country India
Company BrickRed Technologies Pvt. Ltd.
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.  
Verification Code   
Section sponsored by