Thread.Abort in ASP.NET app causes w3wp.exe to crash

后端 未结 4 1450
萌比男神i
萌比男神i 2020-12-30 11:55

Please do not set duplicate flag on this qustion - it is not about \"why ThreadAbortException occurs\", it is about \"why w3wp.exe process terminates after

4条回答
  •  攒了一身酷
    2020-12-30 12:25

    So far I have the only one solution:

        static class WebExtensions
        {
            public static void EndSafe(this HttpResponse response)
            {
                response.Flush();
                response.SuppressContent = true;
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
    
            public static void RedirectSafe(this HttpResponse response, string url)
            {
                response.Redirect(url, false);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }
    

    This is however forces me to ensure that there will be no code executed after it:

    ...some code
    response.RedirectSafe(url);
    return; //<-- important
    ...some more code
    

    Pay attention, that only "return" is not enough in some cases (for example with recursive calls) and in some cases you may need to avoid using "return" (with try-finally constructions)

提交回复
热议问题