我遇到了一个特别有趣的情况。我目前有一个通用的错误处理例程。最近我注意到一个有点奇怪的行为:HttpApplication.Error
会启动,但HttpContext.Current
会为空。这是我的 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...]
}
}
}
有趣的事件看起来像这样:
我的第一个猜测是异常是在上下文外的线程中引发的。
无论如何,我怎样才能正确地捕获它?