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     2879
  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 Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page

Rate this article

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


Write your comment here.
Verification Code