October 15, 2007

Handling SoapExceptions

SoapExceptions are thrown from WebServices when a problem occurs. The SoapException will occur whenever any type of Exception occurs in the WebServices code
e.g. if an IOException occurs it will bubble up to the caller as a SoapException, unlike other Exceptions the origninal Exception is not easily extracted, for instance by using the InnerException property.

Because of this you must handle the SoapException in the sender and the caller. Yuo must stuf your Originals details into a new instance of a SoapException and then throw it. The original Exception's details are contained in the Details property of the SoapException but you still have to do some work to extract it.
Also remember that WebServices return XML, the SoapException is also returned as XML, so our extraction code will need to know how to identify the inner exception and how to extract it from the correct XML Element.

One thing you may wish to do with the SoapException is extract the original Exceptions details. Here's how to achieve this:
First you must catch the SoapException
try
{
}
catch(System.Web.Services.Protocol.SoapException se)
{
//Somehow extract the details from the SoapException, I'll explain how to do this now
}

In here you must extract the SoapExceptions details. This is not as easy as a normal Exception, really to get the details you have to know the format in which they were entered. So you should throw the SoapException with the Details you want from within the WebService yourself.

You do this by stuffing entries into the Details property of the SoapException before throwing it.
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml.Serialization;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SoapExceptionSupplier : System.Web.Services.WebService
{
public SoapExceptionSupplier () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld()
{

try
{
throw new System.IO.IOException("An IOException has been thrown");
}

catch(Exception e)
{
// Build the detail element of the SOAP fault.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);

// Build specific details for the SoapException.
// Add first child of detail XML element.
System.Xml.XmlNode message = doc.CreateNode(XmlNodeType.Element, "Message", "http://tempuri.org/");
message.InnerText = e.GetType().ToString();
// Append the two child elements to the detail node.
node.AppendChild(message);

// Build specific details for the SoapException.

// Add first child of detail XML element.
System.Xml.XmlNode details = doc.CreateNode(XmlNodeType.Element, "Details", "http://tempuri.org/");
details.InnerText = e.Message;
// Append the two child elements to the detail node.
node.AppendChild(details);
SoapException se = new SoapException("Fault occurred", SoapException.ClientFaultCode, Context.Request.Url.AbsoluteUri, node);
throw se;

}
return null;
}

}


The code above is a WebMethod, I've thrown a System.IO.IOException, this is caught and a SoapException is thrown instead, the reason for this is that I am actually embedding Details in the SoapException before it's thrown, if I simply threw the
System.IO.IOException it would be wrapped in a SoapException by the WebService and I would find it very difficult to get any information from it on the Callers side.

In the Catch I construct a XML Document and create a XMLNode, this mode is populated with Details of the Exception thrown, I then pass the XMLNode into the SoapException and throw it.
Now on the Callers side I know what to expect and can check the SoapException for these details using the SoapExceptions Details property.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Windows.Forms;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
localhost.SoapExceptionSupplier soapExSupplier = new localhost.SoapExceptionSupplier();
try
{
//Call the WebMethod I listed earlier
soapExSupplier.HelloWorld();

}
catch (System.Web.Services.Protocols.SoapException soapEx)
{
XmlDocument doc = new XmlDocument();

//First the Error message (Exception type)
MessageBox.Show("Error Message : " + soapEx.Message);

//Now the Detail, more detail on the exception.
doc.LoadXml(soapEx.Detail.OuterXml);
XmlElement root = doc.DocumentElement;
string result =string.Empty;

foreach (XmlElement child in root.ChildNodes)
{
result += child.InnerText;
}

MessageBox.Show("Error Detail : " + result);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

Here I catch the SoapException just thrown by the WebMethod. I construct a XMLDocument from the SoapExceptions Detail properties OuterXML. I extract each XmlElement from this XmlDocument, this will contain the contents I stuffed into the SoapException in the WebService.
Done.

No comments: