Является ли Response.End () вредным?
Эта статья KB говорит, что ASP.NETResponse.End()
прерывает поток
Отражатель показывает, что это выглядит так:
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()
не будет казнен, и это нарушает принцип наименьшего удивления. Это почти какApplication.Exit()
в приложении WinForms. Исключение прерывания потока вызваноResponse.End()
не ловится, поэтому окружение кода вtry
...finally
не удовлетворит.
Это заставляет меня задуматься, стоит ли мне всегда избегатьResponse.End()
.
Может ли кто-нибудь предложить, когда я должен использоватьResponse.End()
, когдаResponse.Close()
и когдаHttpContext.Current.ApplicationInstance.CompleteRequest()
?
ссылка:Рик Страл запись в блоге.
На основании полученного мною ввода мой ответ:Yes, Response.End
is harmful, но это полезно в некоторых ограниченных случаях.
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.
Изменить - 13 апреля 2011
Дополнительная ясность доступна здесь:
- Полезный пост в блоге MSDN
- Полезный анализ Джон Рид