2010-03-04 12 views
23

Ich versuche, die Antwort mit Context.Response.End zu schließen, aber Fehler "Thread was being aborted" erhalten.Context.Response.End() und Thread wurde abgebrochen

Wie schließe ich die Antwort richtig, ohne eine Ausnahme auszulösen?

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) { 

} 

von mir gelöst, sollte der Code aussehen

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

} 
catch (Exception err) { 
} 
+0

beurteilen Sie die respose.end in einem Try-Catch-Block haben? – Andrew

+0

Ich habe meinen Code hinzugefügt. Ja, ich habe Context.Response.End(); Innerhalb Try/catch block und wie Sie sehen, gibt es Haupt Try/catch-Block, der auch Catch-Fehler "Thread wurde abgebrochen". – Tomas

+1

von mir gelöst, sollte der Code aussehen versuchen { } catch (Threadabort err) { } catch (Exception err) { } – Tomas

Antwort

26

einen bestimmten Grund Gibt es Sie nicht context.ApplicationInstance.CompleteRequest() stattdessen verwenden?

Diese Methode wird Kurzschluss der ASP.NET-Pipeline (mit Ausnahme des Endrequest-Ereignisse), ohne die ThreadAbortException zu werfen, damit Sie nicht brauchen den zusätzlichen try/catch Block, und Sie werden auch eine bessere Leistung erleben.

+1

[MSDN-Artikel erklären dieses] (http: // support .microsoft.com/kb/312629/de-de) – snumpy

+1

Ich habe es gerade versucht (ein bisschen spät zur Party). Es schien nicht richtig zu funktionieren: sicherlich hat es nicht die Ausnahme geworfen, die Response.End() warf, aber es führte dazu, dass der Inhalt der .aspx-Seite an das angefügt wurde, was ich bereits in Response.OutputStream geschrieben hatte . Eine Möglichkeit wäre, keinen Inhalt in der ASPX-Datei zu haben, aber ich habe damit den Inhalt für Fehler angezeigt. – Kate

2

Fehler: Thread wurde abgebrochen. bei System.Threading.Thread.AbortInternal() bei System.Threading.Thread.Abort (Object stateInfo) bei System.Web.HttpResponse.End()

Dieser Fehler tritt hauptsächlich auf, wenn Sie Response.End, Response.Redirect verwenden , oder Server.Transfer

Ursache: Die Response.End-Methode beendet die Seitenausführung und verschiebt die Ausführung auf das Application_EndRequest-Ereignis in der Ereignispipeline der Anwendung. Die Codezeile, die Response.End folgt, wird nicht ausgeführt.

Dieses Problem tritt in den Methoden Response.Redirect und Server.Transfer auf, da beide Methoden Response.End intern aufrufen.

Auflösung/Lösung:

Sie einen try-catch-Anweisung verwenden, können diese Ausnahme

oder

Für Response.End, rufen Sie die HttpContext.Current.ApplicationInstance.CompleteRequest Methode zu fangen statt Response.End, um die Codeausführung für das Application_EndRequest-Ereignis zu umgehen. Verwenden Sie für Response.Redirect eine Überladung, Response.Redirect (String url, bool endResponse), die für den Parameter endResponse false übergibt, um den internen Aufruf von Response.End zu unterdrücken. Zum Beispiel: ex: Response.Redirect ("nextpage.aspx", false); Wenn Sie diese Problemumgehung verwenden, wird der Code ausgeführt, der Response.Redirect folgt. Verwenden Sie für Server.Transfer stattdessen die Server.Execute-Methode.

7

Versuchen Sie response.OutputStream.Close(); anstelle von response.End(); Es wird helfen!

+0

Arbeitete für mich! Vielen Dank! – Flea

0

Ich empfehle diese Lösung:

  1. Verwenden Response.End nicht();

  2. Declare diese globale var: bool isFileDownLoad;

  3. Gleich nach Ihrer (response.Write (sw.ToString());) set ==> isFileDownLoad = true;

  4. Ihre außer Kraft setzen Render wie:

    /// /// AEG: Sehr wichtig, den Faden abgebrochen Ausnahme /// /// override protected void Render (Htmltextwriter w) zu handhaben { if (! IsFileDownLoad) base.Render (w); }

0

Oder Sie legen die context.Response.End() innerhalb eines schließlich bloc. Auf diese Weise müssen Sie sich nicht um eine unerwünschte ThreadAbortException kümmern oder eine echte ThreadAbortException ignorieren (was schlecht ist). Sie werden Pipelinestufen auch nicht ignorieren.

try 
{ 
    context.Response.ContentType = "application/json"; 
    context.Response.ContentEncoding = Encoding.UTF8; 

    if (NotAuthorized()) 
    { 
     context.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; 
     return; 
    } 

    context.Response.Write(MakeJsonStuff()); 
} 
catch (Exception ex) 
{ 
    LogException(ex); 

    context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; 
    context.Response.Write(MakeJsonError(ex)); 
} 
finally 
{ 
    context.Response.End(); 
} 
0

Das half mir Thread was being aborted Ausnahme,

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); 
    } 
    } 

zu handhaben, wenn Sie die nach dem folgenden Code statt HttpContext.Current.Response.End() verwenden, werden Sie Server cannot append header after HTTP headers have been sent Exception.

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

Ein weiterer Fix, den ich gefunden ist 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(); 
     } 
    } 

Hoffe, es hilft

Verwandte Themen