Skip to main content

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.

Comments

Popular posts from this blog

Installer CustomAction, Debugging the CustomAction, InstallState

Custom Action The Custom Action is added to the Setup Project, select the Project node and hit the Custom Action button. This allows you add an Action to a particular phase in the Installation. But first you must create the Custom Action. To Add a Custom Action you must first have a Custom Action created, this is usually in the form of a Installer Class, this should be created in a seperate project, the Installer Class is actually one of the File Templates in the C# Projects. So it's File->New Project and select Visual C# Projects. Then add a Class Library, this will prompt you for the Class Library Types , select "Installer Class". Walkthrough - Creating Custom Action (msdn). Also here's a more comprehensive document on Setup/Installer implementations, it delves into the Registry etc Getting Started with Setup Projects (SimpleTalk). Visual Studio Setup Projects and Custom Actions (Simple Talk). Create your Installer Class and then add it as a Custom Action to the ...

dotNET - Debugging

Debugging with .NET MSIL assemblies Visual Studio and debugging the CLR are different, I'll talk about both. MSIL Assemblies Assemblies compiled with .NET tools such as the CLR compiler are compiled into a file which contains MSIL (Microsoft Intermediate Language). At runtime the contents of the assembly are loaded into the CLR and ran as machine code. When you compile an assembly in debug a PDB file is generated alongside the DLL or EXE you've just created. The link between these 2 files is that the PDB contains the line numbers of the methods and classes as well as the file names of the original source code that created the assembly. When you launch the debugger in Visual Studio the assembly is loaded into the Debugger (similar to the CLR) along with the PDB file. The debugger now uses your PDB file contents to match the running code found in the assembly to locations in source files (hopefully in your present project). CLR CLR Inside Out (msdn magazine) .NET Framework Tools:...

BootStrapper BootStrapping

Bootstrapping is the creation of a wrapper installer around already existing installars or files. It's useful if you have multiple installation files that you'll like the user to install in on step. The Bootstrapper itself is a .exe installer file, it also may have .SED file which contains details of the contents of the .exe. There are a few applications out there to generate a bootstrapper, the most simple I've seen is the an app called IEXpress . IExpress Is actually installed on windows (System32/iexpress.exe). This creates an installer, you tell it what files you wish it to include in the installation. I've only played with this so far and it appears to me that the isntaller can only install upto 2 setup.exes. You can include as many files as you like, but the files cannot have the same name, because of this you will probably need to rename your setup.exes to something else because you cannot have 2 files with the same name. After you've included all the files...