Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Response.End() considered harmful?
    primarykey
    data
    text
    <p><a href="http://support.microsoft.com/kb/312629" rel="noreferrer">This KB Article</a> says that ASP.NET's <code>Response.End()</code> aborts a thread. </p> <p>Reflector shows that it looks like this: </p> <pre><code>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(); } } } </code></pre> <p>This seems pretty harsh to me. As the KB article says, any code in the app following <code>Response.End()</code> will not be executed, and that violates the principle of least astonishment. It's almost like <code>Application.Exit()</code> in a WinForms app. The thread abort exception caused by <code>Response.End()</code> is not catchable, so surrounding the code in a <code>try</code>...<code>finally</code> won't satisfy. </p> <p>It makes me wonder if I should always avoid <code>Response.End()</code>.</p> <p>Can anyone suggest, when should I use <code>Response.End()</code>, when <code>Response.Close()</code> and when <code>HttpContext.Current.ApplicationInstance.CompleteRequest()</code>? </p> <p>ref: <a href="http://west-wind.com/weblog/posts/368975.aspx" rel="noreferrer">Rick Strahl's blog entry</a>.</p> <hr> <p>Based on the input I've received, my answer is, <strong>Yes, <code>Response.End</code> is harmful</strong>, but it is useful in some limited cases.</p> <ul> <li>use <code>Response.End()</code> as an uncatchable throw, to immediately terminate the <code>HttpResponse</code> in exceptional conditions. Can be useful during debugging also. <em>Avoid <code>Response.End()</code> to complete routine responses</em>. </li> <li>use <code>Response.Close()</code> to immediately close the connection with the client. Per <a 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" rel="noreferrer">this MSDN blog post</a>, this method <em>is not intended for normal HTTP request processing.</em> It’s highly unlikely that you would have a good reason to call this method.</li> <li>use <code>CompleteRequest()</code> to end a normal request. <code>CompleteRequest</code> causes the ASP.NET pipeline to jump ahead to the <code>EndRequest</code> event, after the current <code>HttpApplication</code> event completes. So if you call <code>CompleteRequest</code>, then write something more to the response, the write will be sent to the client. </li> </ul> <hr> <p>Edit - 13 April 2011</p> <p>Further clarity is available here:<br> - <a 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" rel="noreferrer">Useful post on MSDN Blog</a><br> - <a href="http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx" rel="noreferrer">Useful analysis by Jon Reid</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload