Wednesday, November 5, 2008

SoapExceptions and Global Exception Logging

In all my web sites my pages inherit from a custom base page that inherits from System.Web.UI.Page and overrides OnError.  This method then uses Server.GetLastError() to get the exception generated by the offending code.  At this point, it's probably too late to do anything about it, but at least I can take a snap shot of the current user context, log that information present a friendly message to the user.  Although the bug made it into production at least it's not happening 200 times a day and I don't know about.

A good portion of my site is driven by web services via AJAX and as I start to implement Silverlight for a few sophisticated UI components, I'm relying more and more on web services.  My problem until now was I didn't have any sort of catch-all that notified me about problem in a web service call.

After a bit of research I found something called a SoapExtension in the System.Web.Services.Protocols namespace.

This provides calls at different points in the processing of the soap message:

We can implement some functionality in the AfterSerialize stage.  If we detect a non-null value in the message.Exception property it's probably safe to assume something bad happened within the web service.

The final step is to register this in the web.config file.

In addition SoapExtensions can be associated with specific methods via attributes.  This might be interesting to provide an interesting solution for attaching custom authentication and authorization.


No comments:

Post a Comment