How to create a global exception handler for a Web Service

If you are developing ASP.NET you can easily use the Application_Error event within the Global.asax Syntax file for your global exception handling needs.

However, this approach is not available for Web Services applications. The Application_Error event is not fired if your web method throws an exception. The reason for this is that the HTTP handler for XML Web services consumes any exception that occurs while an XML Web service is executing and turns it into a SOAP fault prior to the Application_Error event is called.

To achieve global exception handling you have to build a SOAP extension. The SOAP extension can check for the existence of an exception in the ProcessMessage method.  

public class SoapExceptionHandler
: System.Web.Services.Protocols.SoapExtension {
 public override void ProcessMessage(
 System.Web.Services.Protocols.SoapMessage message) {
 if (message.Stage == SoapMessageStage.AfterSerialize) {
 if (message.Exception!= null) {
 Logger.Write(message.Exception.InnerException);
 }
 }
 }
 public override object GetInitializer(Type serviceType) {
 return null;
 }
 public override object GetInitializer(LogicalMethodInfo methodInfo,
 SoapExtensionAttribute attribute) {
 return null;
 }
 public override void Initialize(object initializer) {}
}

Obviously, you can do a lot more than just log the Exception. A common approch is to add or remove information to the SoapFault Message to control what data is send over to the client. However, you should override ChainStream if you plan to do so. There is a good posting called . The ChainStream methods allows your SOAP extension to access to the memory buffer containing the SOAP response. Another good article over at  deals with  or take a look at Jan Tielens  article. Once you have your custom soap extension you can either register it in your web.config via the tag or you create an SoapExtensionAttribute to get more fine grained control about the execution of the SoapExtension. The Attribute would look like this:

[AttributeUsage(AttributeTargets.Method)]
public class ExceptionHandlingAttribute: SoapExtensionAttribute
{
public override Type ExtensionType{get { return typeof(SoapExceptionHandler); }}
public override int Priority{get{return 0;}set{}}
}

You can apply this attribute to your XML Web service methods and thus enable your newly created SoapExtension. 

[WebMethod]
[ExceptionHandling]
public void Foo{}
This article is part of the GWB Archives. Original Author: gwbarchive

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.