Is Response.End() considered harmful?

前端 未结 9 765
陌清茗
陌清茗 2020-11-22 01:01

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

Reflector shows that it looks like this:

public void End()
{
            


        
相关标签:
9条回答
  • 2020-11-22 01:55

    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!".

    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.

    0 讨论(0)
  • 2020-11-22 01:56

    I've never considered using Response.End() to control program flow.

    However Response.End() can be useful for example when serving files to a user.

    You have written the file to the response and you don't want anything else being added to the response as it may corrupt your file.

    0 讨论(0)
  • 2020-11-22 01:57

    TL;DR

    Initially I had recommended that you should simply replace all of your calls to [Response.End] with [...] CompleteRequest() calls, but if you want to avoid postback processing and html rendering you'll need to add [...] overrides as well.

    Jon Reid, "Final Analysis"


    Per MSDN, Jon Reid, and Alain Renon:

    ASP.NET Performance - Exception Management - Write Code That Avoids Exceptions

    The Server.Transfer, Response.Redirect, Response.End methods all raise exceptions. Each of these methods internally call Response.End. The call to Response.End, in turn, causes a ThreadAbortException exception.

    ThreadAbortException Solution

    HttpApplication.CompleteRequest() sets a variable that causes the thread to skip past most of the events in the HttpApplication event pipeline [--] not the Page event chain but the Application event chain.

    ...

    create a class level variable that flags if the Page should terminate and then check the variable prior to processing your events or rendering your page. [...] I would recommend just overriding the RaisePostBackEvent and Render methods

    Response.End and Response.Close are not used in normal request processing when performance is important. Response.End is a convenient, heavy-handed means of terminating request processing with an associated performance penalty. Response.Close is for immediate termination of the HTTP response at the IIS/socket level and causes issues with things like KeepAlive.

    The recommended method of ending an ASP.NET request is HttpApplication.CompleteRequest. Keep in mind that ASP.NET rendering will have to be skipped manually since HttpApplication.CompleteRequest skips the rest of the IIS/ASP.NET application pipeline, not the ASP.NET Page pipeline (which is one stage in the app pipeline).


    Code

    Copyright © 2001-2007, C6 Software, Inc as best I could tell.


    Reference

    HttpApplication.CompleteRequest

    Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.

    Response.End

    This method is provided only for compatibility with ASP—that is, for compatibility with COM-based Web-programming technology that preceded ASP.NET.preceded ASP.NET. [Emphasis added]

    Response.Close

    This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. [Emphasis added]

    0 讨论(0)
提交回复
热议问题