Httpmodule: How to handle Httpapplication errors without Httpcontext?

Asked

Viewed 153 times

4

I have been finding a particularly interesting situation. I have a generic error handling routine implemented within a HttpModule, and recently I noticed a strange behavior: The event HttpApplication.Error is fired, but HttpContext.Current is null.

This is the relevant part of 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 event appears as follows:

enter image description here

One possibility is that the Exception is being generated in a thread out of context.

Anyway, how can I intercept this error in order to treat it?

Crosspost: https://stackoverflow.com/questions/26018273/httpmodule-how-to-handle-httpapplication-errors-without-httpcontext

1 answer

0

In the context_Error method you can cast the Sender parameter for Httpapplication and then access the Context property. It may also be an exception that occurs without any HTTP request, for example, web application boot/warmup code.

  • Interesting point, Ricardo! Thanks for the suggestion, I will test the cast for Httpapplication.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.