到Response.End()与HttpContext.Current.ApplicationInstance.CompleteRequest() [英] Response.End() vs HttpContext.Current.ApplicationInstance.CompleteRequest()

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

问题描述

我们的应用程序的用户白天下载附件在两秒钟至少一次。



前面的场景:



我们正在使用到Response.End()中止后连接到客户端用户下载附件。当我们有性能问题,我们就开始记录异常和最重复一次之一是线程中止异常。既然我们都可以从Web服务的附件,我们必须做一些清理,我们不得不清理中的try-catch-finally块。经过一番研究,我已经明白到Response.End之后的任何代码()将不会被即使是在finally块执行。 ?是这样吗



目前的情况:



我读过约到Response.End堆栈溢出线程()是有害的,它需要只在真正需要的时候使用,所以我决定用HttpContext的.... CompleteRequest()代替。有了这个代码,需要清理完成后,但毕竟是呈现的HTML被追加到下载的附件。我试图重写渲染和RaisePostBackEvent在同一篇文章中建议,但问题仍然存在。如何解决此问题的任何想法将是有益的。



代码:

  HttpContext.Current.Response.Clear() ; 

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader(内容处置,附件;文件名=+
文件名);
Response.AddHeader(内容长度,fileContent.Length.ToString());
Response.ContentType =应用程序/八位字节流;
Response.BinaryWrite(fileContent);
Response.Flush();


解决方案

到Response.End 内部抛出 ThreadAbortException 杀的要求 - 如果你需要做一些清理工作,这需要做的的调用到Response.End 而成。



的Response.Redirect 到Response.End 不用try / catch块互动良好。因此,在你的情况,你应该做的一切你的逻辑写在你的try / catch响应流,然后只需调用到Response.End 后您的finally块。


The users of our application download an attachment at least once in two seconds during the day.

Previous Scenario:

We were using Response.End() to abort the connection to the client after the user downloads an attachment. As we were having performance issues, we started logging exceptions and one of the most repeated one was the thread abort exception. Since we are getting the attachment from a web service, We have to do some clean up and we had the clean up in try-catch-finally block. After some research, I have understood that any code after Response.End() will not be executed even if it is in finally block. Is that right?

Current Scenario:

I've read the thread in stack overflow about Response.End() being harmful and it needs to be used only when it is really needed, so I decided to use HttpContext....CompleteRequest() instead. With this code, the clean up needed is done, but the html that is rendered is being appended to the attachment downloaded. I tried overriding the Render and RaisePostBackEvent suggested in the same article but the problem still persists. Any idea on how to resolve this issue would be helpful.

Code:

HttpContext.Current.Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader("Content-Disposition", "attachment; filename=" +   
filename);
Response.AddHeader("Content-Length", fileContent.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(fileContent);
Response.Flush();

解决方案

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.

这篇关于到Response.End()与HttpContext.Current.ApplicationInstance.CompleteRequest()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆