HttpModule: How to handle HttpApplication errors without HttpContext?

1.4k Views Asked by At

I'm running into a particularly interesting situation. I have a generic error handling routine currently working. Recently I've noticed a somewhat odd behavior: HttpApplication.Error would fire up, but HttpContext.Current would be null. This is the relevant bit on my HttpModule:

public void Init(HttpApplication context)
{
    context.Error += context_Error;
    context.PostMapRequestHandler += context_PostMapRequestHandler;

}

void context_PostMapRequestHandler(object sender, EventArgs e)
{
    var aux = HttpContext.Current.Handler as Page;
    if (aux != null) aux.Error += context_Error;
}

void context_Error(object sender, EventArgs e)
{
    _localLog.Add("HttpApplication error handler reached.");
    try
    {
        if (HttpContext.Current == null)
        {
            _localLog.Add("No HttpContext.");
        }
        else
        {
            var objError = HttpContext.Current.Server.GetLastError();

            if (objError == null)
            {
                _localLog.Add("GetLastError(): no error.");
                return;
            }

            //[Proper error handler follows here...]
        }
    }
}

And the intriguing event would look like this:

enter image description here

My first guess is that the exception is being thrown in an out-of-context thread.

In any case, how can I trap it properly?

0

There are 0 best solutions below