Response.End() VS Context.ApplicationInstance.CompleteRequest()

Response.End() VS Context.ApplicationInstance.CompleteRequest()

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.
https://msdn.microsoft.com/en-us/library/ff647787.aspx#scalenetchapt06_topic22 性能第六章
Server.Transfer, Response.Rediret, Response.End 方法都会产生异常。每种发份都会调用Response.End,产生ThreadAbortedException异常

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.
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
http://web.archive.org/web/20101224113858/http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx
HttpApplication.CompleteRequest() 设置的变量能使线程跳过大部分HttpApplication 管道事件,而不是页面事件链,而是Application 事件链
当性能重要时,Response.End 和 Response.Close 不用于正常的请求处理。Response.End 是便捷的重型的方法来终止与性能消耗有关的请求处理.
Response.Close 立即终止IIS/socket级别的HTTP 响应,并导致像 Http 的 KeepAlive问题.
建议终止ASP.NET request方法是HttpApplication.CompleteRequest。 铭记于心的是

1. HttpResponse.End flushes the output buffer to the client and terminates the current request-handling thread , whereas HttpApplication.CompleteRequest tells ASP.NET to immediately skip all future stages in the ASP.NET pipeline and jump directly to the EndRequest step (which also raises the HttpApplication.EndRequest event. The request thread then proceeds with normal end-of-life cleanup.
So, Response.End is like an ejector seat: it quickly ends things, but means you lose control and might be unnecessarily harsh. Whereas CompleteRequest is like making an emergency landing at the nearest airport.

1. Response.End internally throws the ThreadAbortException to kill the request - if you need to do some kind of cleanup, this needs to be done before the call to Response.End is made.
Response.Redirect and Response.End do not interact well with try / catch blocks. So in your situation, you should do all your logic writing to the response stream in your try / catch, then simply call Response.End after your finally block.

1. create pdf
Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.End()

参考:http://weblogs.asp.net/hajan/why-not-to-use-httpresponse-close-and-httpresponse-end

原文地址:https://www.cnblogs.com/dennysong/p/5430478.html