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

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

问题描述

此知识库文章说,ASP.NET的到Response.End()中止线程。

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

反射显示,它看起来是这样的:

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

这似乎是pretty恶劣的给我。由于KB文章说,继任何code在应用到Response.End()将不被执行,违反最小惊讶的原则。这几乎就像 Application.Exit()的WinForms应用程序。致到Response.End()不开捕,所以围绕在尝试 ... 最后将无法满足。

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:里克施特拉尔的博客文章

,我的回答是,到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()作为一个不可捕获的抛出,立即终止的Htt presponse 中得天独厚的条件。可调试期间也很有用。 避免到Response.End()来完成日常的响应的。

  • 使用 Response.Close()立即关闭与客户端的连接。每<一个href=\"http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx\">this MSDN博客帖子,这种方法的不用于正常的HTTP请求处理。的这是极不可能的,你将有一个很好的理由来调用这个方法。

  • 使用 CompleteRequest()来结束一个正常的请求。 CompleteRequest 导致ASP.NET管道提前跳到 EndRequest 事件,目前在的HttpApplication 事件完成。所以,如果你叫 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

进一步明晰,请访问:结果
- <一个href=\"http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx\">Useful张贴在MSDN博客结果
- 乔恩·里德 有用的分析

Further clarity is available here:
- Useful post on MSDN Blog
- Useful analysis by Jon Reid

推荐答案

如果你雇用了您的应用程序异常记录仪,它会被淡化与 ThreadAbortException 取值从这些良性到Response.End()来电。我认为这是说,微软的路别吵了!

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天全站免登陆