Thursday, February 17th, 2011

WCF handles errors and convey these details to the client using Fault contracts. SOAP fault messages are included in the metadata for an operation contract and when the exception is raised in these methods create a fault for the clients to get more details about the exceptions. But when you have a client that runs on 2.0 version of .NET framework, they can’t make use of these fault contracts. In this case you need to send your faults as SOAP exceptions as in Web services.

For e.g.

public void MyServiceMethod()




//Application Logic


catch (System.Security.SecurityException securityException)


throw GenerateSoapException(“ServiceMethodName”, “YourNamespace”, securityException.Message, “1000”, securityException.Source, ServiceFaultLocation.FromServerCode);



private SoapException GenerateSoapException(string uri,

string serviceNamespace,

string message,

string errNum,

string source,

ServiceFaultLocation faultLocation)


XmlQualifiedName faultCodeLocation = SetFaultCode(faultLocation);

XmlDocument xmlDoc = new XmlDocument();

XmlNode rootNode = CreateExceptionNode(serviceNamespace, message, errNum, source, xmlDoc);

SoapException soapException = new SoapException(message, faultCodeLocation, uri, rootNode);

return soapException;


private XmlNode CreateExceptionNode(string serviceNamespace, string message, string errNum, string source, XmlDocument xmlDoc)


XmlNode rootNode = xmlDoc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);

XmlNode errorNode = PopulateErrorDetails(serviceNamespace, message, errNum, source, xmlDoc);


return rootNode;


private XmlNode PopulateErrorDetails(string serviceNamespace, string message, string errNum, string source, XmlDocument xmlDoc)


XmlNode errorNode = xmlDoc.CreateNode(XmlNodeType.Element, “Error”, serviceNamespace);

XmlNode errorNumberNode = xmlDoc.CreateNode(XmlNodeType.Element, “ErrorNumber”, serviceNamespace);

errorNumberNode.InnerText = errNum;

XmlNode errorMessageNode = xmlDoc.CreateNode(XmlNodeType.Element, “ErrorMessage”, serviceNamespace);

errorMessageNode.InnerText = message;

XmlNode errorSourceNode = xmlDoc.CreateNode(XmlNodeType.Element, “ErrorSource”, serviceNamespace);

errorSourceNode.InnerText = source;




return errorNode;


private XmlQualifiedName SetFaultCode(ServiceFaultLocation code)


XmlQualifiedName faultCodeLocation = null;

switch (code)


case ServiceFaultLocation.FromClientCode:

faultCodeLocation = SoapException.ClientFaultCode;


case ServiceFaultLocation.FromServerCode:

faultCodeLocation = SoapException.ServerFaultCode;



return faultCodeLocation;


Later in your client you can use this SOAP exception and parse the XML message to get the details of the exception.

Category: .Net | Tags: ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses

  1. Hi Prajeesh,

    nice blog, 1 suggestion 4u ‘format/indent the code’ so that readability is good. take snapshot of code in IDE, attach the image

  2. Thanks Dhanya,
    Sure will make a note of this next time 🙂

Leave a Reply


This blog is kept spam free by WP-SpamFree.