Skip to content

Instantly share code, notes, and snippets.

@htuomola
Created May 22, 2012 10:45
Show Gist options
  • Save htuomola/2768271 to your computer and use it in GitHub Desktop.
Save htuomola/2768271 to your computer and use it in GitHub Desktop.
Get error details from ASMX web service FaultException in WCF
public static string GetErrorMessage(FaultException faultException)
{
string errorMessage;
MessageFault messageFault = faultException.CreateMessageFault();
if (messageFault.HasDetail)
{
var xElement = messageFault.GetDetail<XmlElement>();
string code = GetXmlNodeText(xElement, "/code");
string description = GetXmlNodeText(xElement, "/description");
errorMessage = string.Format("Error message: {0}, Code: {1}, Description: {2}",
faultException.Message, code, description);
}
else
errorMessage = faultException.Message;
return errorMessage;
}
private static string GetXmlNodeText(XmlNode xElement, string xpath)
{
XmlNode node = xElement.SelectSingleNode(xpath);
return node != null ? node.InnerText : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment