Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IHttpModule Over Global.asax

I've been given the thrilling task of re-writing our exception handling system. Whilst I will state that handling exceptions from an application-wide point of view isn't something we want, typically it's unavoidable when our team are understaffed for the sheer amount of work we need to push out the door, so please, no flaming the globalised solution to exception handling here :)

I've had a good hunt to see what common solutions exist. At the moment we use Global.asax with the Application_Error event to do Server.GetLastError() which is placed in Session state then a redirect is called to another page where the session data is then retrieved and output in a human readable format. The redirect also calls a sproc which will carefully audit the error information which is a) e-mailed to the developers and b) viewed from a web page only viewable by developers.

The new way I've seen of doing things is using the IHttpModule interface using a class in App_Code to do something along these lines (this is my quick implementation)

Imports Microsoft.VisualBasic

Public Class ErrorModule : Implements IHttpModule

  Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    ' Not used
  End Sub

  Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.Error, AddressOf context_Error
  End Sub

  Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = HttpContext.Current.Server.GetLastError

    ' do something with the error
    ' call the stored procedure
    ' redirect the user to the error page

    HttpContext.Current.Server.ClearError()
    HttpContext.Current.Response.Redirect("index.htm")

  End Sub
End Class

My question is, what is the benefit of this solution over using Global.asax events? Additionally, what is the best way to hand the data to an error page?

EDIT: The code above does work by the way ;)

EDIT: Also, how does the HttpModule work behind the scenes? Does it just register the Error event to that particular function on application start?

UPDATE:

Upon much further investigation it seems grabbing session data is really, really messy when it comes to using IHttpModule interface. I don't think MS have matured HttpModule enough for it to be used in our particular scenario - until there are events specific to session data it's too dangerous for us to use.

like image 573
Kezzer Avatar asked Mar 04 '09 13:03

Kezzer


2 Answers

Using a module has the advantage of being easily removable, all you need to do to disable it is to remove it from <httpModules> in your config.

As far as your data goes, try going with Server.Transfer or Server.RewritePath - that will keep all the current data (including the last server error).

If for some reason it clears the last error, you can save the error to HttpContext.Items before the transfer/rewrite and then retrieve it afterwards.

Edit: In response to your edit, an IHttpModule attaches to any appropriate events in it's IHttpModule.Init implementation.

like image 161
Richard Szalay Avatar answered Oct 14 '22 13:10

Richard Szalay


HttpModule basically does the same thing as Global.asax. It's designed as a more reusable and self-contained module for event handling.

like image 31
mmx Avatar answered Oct 14 '22 11:10

mmx