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?