Context.Response.End()和线程已被中止 [英] Context.Response.End() and Thread was being aborted

查看:1564
本文介绍了Context.Response.End()和线程已被中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用时关闭响应 Context.Response.End ,但收到错误线程已被中止

我如何正确地关闭而没有引发异常的反应?

  {尝试
   Context.Response.Clear();
   Context.Response.ContentType =text / html的;
   //Context.Response.ContentType =应用/ JSON;
   JsonObjectCollection收集=新JsonObjectCollection();
   collection.Add(新JsonNumericValue(结果code,1));
   collection.Add(新JsonStringValue(sourceurl,exchangeData.cUrl));
   collection.Add(新JsonStringValue(文件名,文件名));
   collection.Add(新JsonStringValue(文件大小,档案大小));
   collection.Add(新JsonStringValue(fileurl,Common.GetPDFURL + outputFileName));
   JsonUtility.GenerateIndentedJsonText =真;
   Context.Response.Write(集合);
  尝试{
     Context.Response.End();
  }赶上(EXC ThreadAbortException){
     //这应该是前即一般异常第一个catch块
     //这Catch块是吸收由异常抛出到Response.End
  }
}赶上(例外错误){}

由我自己解决

中,code应该像

  {尝试
  Context.Response.End();
}赶上(ThreadAbortException ERR){}
赶上(例外错误){
}


解决方案

有没有使用 context.ApplicationInstance.CompleteRequest(),而不是一个具体的原因是什么?

此方法将短路ASP.NET管道(除EndRequest事件)没有抛出的 ThreadAbortException 这样你就不会需要额外的尝试 / 块,你会也体验更好的性能。

I am trying to close the response using Context.Response.End but receive error "Thread was being aborted".

How do I correctly close the response without triggering an exception?

try {
   Context.Response.Clear();
   Context.Response.ContentType = "text/html"; 
   //Context.Response.ContentType = "application/json";
   JsonObjectCollection collection = new JsonObjectCollection();
   collection.Add(new JsonNumericValue("resultcode", 1));
   collection.Add(new JsonStringValue("sourceurl", exchangeData.cUrl));
   collection.Add(new JsonStringValue("filename", fileName));
   collection.Add(new JsonStringValue("filesize", fileSize));
   collection.Add(new JsonStringValue("fileurl", Common.GetPDFURL + outputFileName));
   JsonUtility.GenerateIndentedJsonText = true;
   Context.Response.Write(collection);
  try {
     Context.Response.End();
  } catch (ThreadAbortException exc) {
     // This should be first catch block i.e. before generic Exception
     // This Catch block is to absorb exception thrown by Response.End
  }
} catch (Exception err) {

}

Solved by myself, the code should look like

try {
  Context.Response.End();
} catch (ThreadAbortException err) {

}
catch (Exception err) {
}

解决方案

Is there a specific reason you aren't using context.ApplicationInstance.CompleteRequest() instead?

This method will short circuit the ASP.NET pipeline (except for the EndRequest event) without throwing the ThreadAbortException so you won't need the extra try/catch block, and you will also experience better performance.

这篇关于Context.Response.End()和线程已被中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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