How do you ensure that AppDomain event handlers in HttpModule stay alive?

Last post 11-10-2007 10:20 AM by bitmask. 1 replies.

Sort Posts:

  • How do you ensure that AppDomain event handlers in HttpModule stay alive?

    11-08-2007, 9:47 AM

    Hi,
    I've got a HttpModule that providers an event handler to the AppDomain event UnhandledException.
    This is specifically to catch any worker thread exceptions that occur outside the context of an asp.net request.
    (In 2.0 such errors kill the application immediately with event log error).

    Here is a snippet of the module with Init code:
     

    static int unhandledExceptionCount = 0;
    static object lockObject = new object();
    static bool initialized = false;

    public void Init(HttpApplication context)
    {
    // We only want one single instance of a HttpModule to subscribe to the unhandled event.
    // This is because the event is not called inside a HttpApplication (and thus one corresponding Module)


    if( !initialized)
    {
    // create a lock so no other thread/HttpModule instance can affect the static variable
    lock (lockObject)
    {
    if( !initialized)
    {
    AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    initialized = true;
    }
    // now lock is released and the static variable is true.. we have attached the event handler to one instance
    }
    }
    }
      

    Because there are multiple HttpApplication objects pooled within an AppDomain, there will be multiple HttpModules - one for each HttpApplication object.
    So the above code is ensuring that only one of the modules can register the eventhandler and is therefore thread/multiple instance aware..

    Now this works great and has been unit tested with some success.

    However, my question is... if some of the HttpApplication pool objects are disposed of when the runtime deems them unnecessary (e.g the site was busy for some time, and is now quiet), how can we know if the HttpModule that has the handler is still alive?

    If the HttpApplication containing the insance of the HttpModule that first hooked up the event handler is killed off, there will be nothing to catch the AppDomain.UnhandledException...

    Does anyone have any ideas on this?

    Should we use the Dispose method of the HttpModule instance that has the eventhandler and release the "initialized" static bool so another can register when it starts up?

    Or is something that we don't have to worry about? Will the original HttpApplication/HttpModule be kept alive?

    Regards,
    foreachbiscuit
    blog @ http://foreachbiscuit.wordpress.com
    Filed under: ,
  • Re: How do you ensure that AppDomain event handlers in HttpModule stay alive?

    11-10-2007, 10:20 AM
    Answer
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 11:18 AM
    • Citizen of the Earth
    • Posts 1,228

    I don't think there is any guarantee that a specific HttpModule instance will remain in the processing pipeline for the life of the application. The object could get disposed - which could give you trouble.

    Remember, though, that disposed isn't the same as removing the object from memory. Since you are storing a delegate with a reference to your object in a global area ( AppDomain.CurrentDomain.UnhandledException), the garbage collector won't be able to take your object away. I'd probably create a new, non-HttpModule derived class with the single responsibility of listening for unhandled exceptions, so I woulnd't have to worry about being disposed.

    Scott
    http://www.OdeToCode.com/blogs/scott/
Page 1 of 1 (2 items)