Oct 31

Mike Weerasinghe has made a good question regarding my previous post about error handling. Mike says that he”s handling errors by using a module which handles the HttpApplication error event. Unfortunately, this will stop working when you use AJAX extensions because the ScriptManager control will handle the error and automatically return a response to the client stopping the event from being propagated to the HttpApplication object. So, if you”re handling the Error event of that class, you won”t be notified about the error and your logging will stop working.

The solution to this is to handle the Error of the Page since the methods that handle it will always be called if you get an exception during a partial postback. Since I think nobody will want to add their logging logic to all the pages and will probably want to keep handling the error from a module, then there”s a workaround which might just work with you. What you need is to handle the PostMapRequestHandler event, which is fired by the HttpApplication object after mapping the current request to a handler. During that event, you can check for the current handler and see if it”s a page. If it is, then you just need to handle the Error event and you should be ready to go. Here”s a sample which shows what you need in order for it to work (I”m pasting the code from global.asax, but it should also work from a module though i haven”t tested that):

protected void Application_PostMapRequestHandler(object sender, EventArgs e)
  Page aux = this.Context.Handler as Page;
  if( aux != null )
        aux.Error += new EventHandler(page_Error);

void page_Error(object sender, EventArgs e)
   //log error as you want

By hooking the error event like this, your method will always be called before the one introduced by ScriptManager and you will have access to the current exception. Just don”t clear it because if you do,the ScriptManager”s method will not get the info it need to its work.

Hope this helps…

Update: I was so eager to get a general error handling solution that i”ve just forgot about object lifetime! As my good friend João Paulo said in the comments,my previous solution might be bad if you don”t want to leak resources (and yes, I do believe that you don”t). As he said,  I should remove my event handler so that I don”t keep the page alive. However, there”s a problem with the PostRequestHandlerExecute event handling: though I could be mistaken, but I think it won”t be called due to the way that ScriptManager handles the Error event generated by the page: it calls Response.End after packaging the response which is being sent back to the client. So, removing the method from the event list during the page_Error method might be the best option to this scenario (specially if you know that the page has a ScriptManager control)

btw, thanks for the correction João…and don”t be a stranger! 🙂

Update2: re-reading my previous update made me think that I didn”t explain myself correctly. My previous remarks are only valid for when an exception happens in a page that has a ScriptManager control. If there isn”t an exception, you still should hook a post execute handler event in order to unsubscribe the event handling…

3 comments so far

  1. Joao Paulo Carreiro
    12:46 pm - 10-31-2006

    Won”t this cause a “memory leak” on the application?

    You”re keeping the pages “live” by having a event hook up but by never removing it.

    Why not use PreRequestHandlerExecute to hookup and PostRequestHandlerExecute to remove the hook?

  2. Lucian N
    10:34 am - 2-2-2007

    Thx for pointed it out.


  3. Kitu
    6:42 am - 2-22-2007

    i have a page whcih uses partial post backs ,i have some server side validations in the page, if validation fails i used throw an application exception to get the message to the client tha validation has failed. All this is working fine when i am setting the Cutom errors mode “Off” in web.config ,but when i set it to “on” the exception raised is gettign habled some where and i am able to get the exception but with the exception message cleared message.

    Does any body have any suggestions on this problem?

    Thanks in advance