10

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?

OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • I just did a test with your code almost exactly as you have it and I trapped the exception properly. The error is, like you said, being called from an out-of-process task (maybe asynchronous?) – Saturn K Sep 26 '14 at 20:53
  • @KeyvanSadralodabai That's probably the cause, Keyvan - the odd thing is that the `context_error` event is raised anyway. – OnoSendai Oct 15 '14 at 01:29

0 Answers0