Response.End() 被认为是有害的吗? [英] Is Response.End() considered harmful?

查看:32
本文介绍了Response.End() 被认为是有害的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇知识库文章 表示 ASP.NET 的 Response.End() 中止了一个线程.

This KB Article says that ASP.NET's Response.End() aborts a thread.

Reflector 显示它看起来像这样:

Reflector shows that it looks like this:

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();
        }
    }
}

这对我来说似乎很苛刻.正如知识库文章所说,Response.End() 之后的应用程序中的任何代码都不会被执行,这违反了最小惊讶原则.这几乎就像 WinForms 应用程序中的 Application.Exit().由 Response.End() 引起的线程中止异常是不可捕获的,因此在 try...finally 中包围代码不会满足.

This seems pretty harsh to me. As the KB article says, any code in the app following Response.End() will not be executed, and that violates the principle of least astonishment. It's almost like Application.Exit() in a WinForms app. The thread abort exception caused by Response.End() is not catchable, so surrounding the code in a try...finally won't satisfy.

这让我想知道我是否应该总是避免 Response.End().

It makes me wonder if I should always avoid Response.End().

谁能建议我什么时候应该使用 Response.End(),什么时候使用 Response.Close() 以及什么时候 HttpContext.Current.ApplicationInstance.CompleteRequest()?

Can anyone suggest, when should I use Response.End(), when Response.Close() and when HttpContext.Current.ApplicationInstance.CompleteRequest()?

ref: 瑞克Strahl 的博客条目.

根据我收到的输入,我的回答是,是的,Response.End 是有害的,但它在某些有限的情况下很有用.

Based on the input I've received, my answer is, Yes, Response.End is harmful, but it is useful in some limited cases.

  • 使用 Response.End() 作为无法捕获的抛出,在异常情况下立即终止 HttpResponse.在调试期间也很有用.避免使用 Response.End() 来完成常规响应.
  • 使用 Response.Close() 立即关闭与客户端的连接.每 这篇 MSDN 博客文章,此方法不适用于普通的 HTTP 请求处理.您不太可能有充分的理由调用此方法.
  • 使用 CompleteRequest() 结束一个正常的请求.CompleteRequest 使 ASP.NET 管道在当前 HttpApplication 事件完成后跳转到 EndRequest 事件.因此,如果您调用 CompleteRequest,然后在响应中写入更多内容,则写入将发送到客户端.
  • use Response.End() as an uncatchable throw, to immediately terminate the HttpResponse in exceptional conditions. Can be useful during debugging also. Avoid Response.End() to complete routine responses.
  • use Response.Close() to immediately close the connection with the client. Per this MSDN blog post, this method is not intended for normal HTTP request processing. It’s highly unlikely that you would have a good reason to call this method.
  • use CompleteRequest() to end a normal request. CompleteRequest causes the ASP.NET pipeline to jump ahead to the EndRequest event, after the current HttpApplication event completes. So if you call CompleteRequest, then write something more to the response, the write will be sent to the client.

编辑 - 2011 年 4 月 13 日

Edit - 13 April 2011

此处提供了更清晰的信息:

Further clarity is available here:

推荐答案

如果你在你的应用中使用了异常记录器,它将会被来自这些良性 ThreadAbortException 冲淡Response.End() 调用.我认为这是微软说Knock it off!"的方式.

If you had employed an exception logger on your app, it will be watered down with the ThreadAbortExceptions from these benign Response.End() calls. I think this is Microsoft's way of saying "Knock it off!".

我只会使用 Response.End() 如果有一些异常情况并且无法执行其他操作.也许那时,记录此异常实际上可能表示警告.

I would only use Response.End() if there was some exceptional condition and no other action was possible. Maybe then, logging this exception might actually indicate a warning.

这篇关于Response.End() 被认为是有害的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆