0

我已经看到了几个关于这个问题的问题,但我没有找到适合我的问题的答案。最初我在函数中编写 json 后使用以下代码

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();

变得Server cannot append header after HTTP headers have been sent异常。

所以我将代码更改为

try {
    HttpContext.Current.Response.Write(Data);
    HttpContext.Current.Response.End();
} catch (System.Threading.ThreadAbortException exc) {
    try {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    } catch (Exception ex) {
              //Log Exception
    }

}

所有这些代码都在函数中(比如说)writeData(),它由一个名为CallWriteData. 现在该异常已在函数中成功处理,但在父函数中WriteData()抛出异常。Thread was being abortedCallWriteData

老实说,这不是我项目中的主要问题,但如果我能解决这个烦人的问题,那就太好了。CallWriteData此外,并非每次都出现此异常 (有时已成功处理)。

4

2 回答 2

0

最后,这帮助我处理Thread was being aborted异常,

try
{
   //Write HTTP output
    HttpContext.Current.Response.Write(Data);
}  
catch (Exception exc) {}
finally {
   try 
    {
      //stop processing the script and return the current result
      HttpContext.Current.Response.End();
     } 
   catch (Exception ex) {} 
   finally {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        //Suspends the current thread
        Thread.Sleep(1);
     }
   }

如果您使用以下以下代码而不是HttpContext.Current.Response.End(),您将得到Server cannot append header after HTTP headers have been sent异常。

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();

我发现的另一个修复是Thread.BeginCriticalRegion();

   try 
 {
    //Write HTTP output
   HttpContext.Current.Response.Write(Data);
  } catch (Exception exc) {} 
  finally {
    try {
     //Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
     Thread.BeginCriticalRegion();
     HttpContext.Current.Response.End();
         } catch (Exception ex) {} 
    finally {
    //Sends the response buffer
    HttpContext.Current.Response.Flush();
    // Prevents any other content from being sent to the browser
    HttpContext.Current.Response.SuppressContent = true;
    //Directs the thread to finish, bypassing additional processing
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    Thread.EndCriticalRegion();
         }
   }

现在我是一个如释重负的人。

于 2015-09-17T08:29:33.483 回答
-2

This is how IIS works. It starts a new thread for the new request. The thread does it's stuff, and when it ends the thread is aborted. Normally, this happens in the .NET plumbing and it's handles there. However, it you do something like a Server.Redirect() it'll also get aborted - in your code. Similarly with completing the request yourself as you do. IIS says "It's sent the return, so kill it." That's how it works.

(The thread is probably saved for re-use by another request, but the code just finished on it is aborted.)

于 2015-09-14T14:58:36.717 回答