当我使用Response.Redirect(...)将表单重定向到新页面时,我收到错误:
mscorlib.dll中出现"System.Threading.ThreadAbortException"类型的第一次机会异常mscorlib.dll中
出现"System.Threading.ThreadAbortException"类型的异常,但未在用户代码中处理
我对此的理解是,错误是由Web服务器中止调用response.redirect的页面的其余部分引起的.
我知道我可以添加第二个参数Response.Redirect,称为endResponse.如果我将endResponse设置为True,我仍然会收到错误,但如果我将其设置为False,那么我不会.我很确定,这意味着网络服务器正在运行我重定向的页面的其余部分.至少可以说这似乎效率低下.有一个更好的方法吗?除了Response.Redirect或有没有办法迫使旧页面停止加载我不会得到的东西ThreadAbortException?
这篇知识库文章说,ASP.NET Response.End()中止了一个帖子.
反射器显示它看起来像这样:
public void End()
{
if (this._context.IsInCancellablePeriod)
{
InternalSecurityPermissions.ControlThread.Assert();
Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
}
else if (!this._flushing)
{
this.Flush();
this._ended = true;
if (this._context.ApplicationInstance != null)
{
this._context.ApplicationInstance.CompleteRequest();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很苛刻.正如知识库文章所说,以下应用程序中的任何代码Response.End()都不会被执行,这违反了最不惊讶的原则.它几乎就像Application.Exit()在WinForms应用程序中.造成线程终止异常Response.End()不开捕,所以代码周围的try...... finally不会满足.
这让我想知道我是否应该总是避免Response.End().
任何人都可以建议,我什么时候应该使用Response.End(),何时Response.Close()何地HttpContext.Current.ApplicationInstance.CompleteRequest()?
参考:Rick Strahl的博客文章.
根据我收到的输入,我的回答是,是的,Response.End是有害的,但在某些有限的情况下它是有用的.
Response.End()作为一个不可捕获抛出,立即终止HttpResponse在特殊的条件.在调试过程中也很有用. 避免Response.End()完成常规反应.Response.Close()立即关闭与客户端的连接.根据此MSDN博客文章,此方法不适用于正常的HTTP请求处理. 你不太可能有充分的理由来调用这种方法.在过去的几天里,我们看到w3wp.exe工作进程间歇性崩溃,为我们公司网站的主应用程序池提供服务.有时崩溃是孤立的,IIS可以成功重启工作进程.但如果在5分钟内发生超过5次崩溃,IIS Rapid Fail Protection将启动并停止应用程序池.以下是崩溃前应用程序事件日志中的示例条目:
An unhandled exception occurred and the process was terminated.
Application ID: /LM/W3SVC/2/ROOT
Process ID: 3640
Exception: System.Threading.ThreadAbortException
Message: Thread was being aborted.
StackTrace: at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)
at System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)
at System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)
Run Code Online (Sandbox Code Playgroud)
由于ThreadAbortException导致崩溃后立即记录了一个更严重的事件:
Faulting application name: w3wp.exe, version: 8.0.9200.16384, time stamp: 0x5010885f
Faulting module name: KERNELBASE.dll, version: 6.2.9200.17366, time stamp: 0x554d16f6
Exception code: 0xe0434352
Fault offset: 0x00010192
Faulting process id: 0xe38
Faulting …Run Code Online (Sandbox Code Playgroud)