1

我一直在寫針對.NET框架V3.5和Visual Studio的Web應用程序會出現2013年一個StackOverflowException上System.Diagnotics.StackTrace()

間接遞歸在它someties造成StackOverflowException我這麼寫一種檢查堆棧溢出的方法。

public static void CheckStackOverflow() { 
    StackTrace stackTrace = new StackTrace(); 
    StackDepth = stackTrace.GetFrames().Length; 
    if(StackDepth > MAXIMUM_STACK_DEPTH) { 
     throw new StackOverflowException("StackOverflow detected."); 
    } 
} 

的問題是,一個StackOverflowException在第一行中出現,即new StackTrace(),所以我不能照顧它。

我知道調用StackTrace()也會將堆棧加深幾級,所以我明白這可能會發生。然而,有一些耐人尋味:

  1. 選擇了在Visual Studio 2012 的Visual Studio(ASP.NET)開發服務器(以下卡西尼)有沒有問題,所以我的IIS設置或類似的東西是疑似。
  2. 堆棧在發生異常時並不夠深。
  3. 這隻發生在調試。不管配置如何(即調試/發佈)。

編輯:我試圖changed IIS Express settings和它並沒有差異。此外,嘗試本地IIS選項也沒有運氣,無論是。所以,

if(RunningWithVisualStudio) { // Start Debugging or Without Debugging 
    if(UsingCassini) { 
     throw new StackOrverflowException("A catchable exception."); // expected 
    } else { 
     throw new StackOverflowException("I cannot catch this dang exception."); 
    } 
} else { // publish on the identical ApplicationPool. 
    throw new StackOrverflowException("A catchable exception."); // expected 
} 

我想我犯的錯誤配置IIS快遞但我現在完全失去了。

回答

1

下面是我做的事是解決方法:

  1. 下面我加入的.csproj文件來定義IDE的當前版本。 image
  2. Defined DEBUG constant
  3. 加入使用預處理器指令的條件。

    public static void CheckStackOverflow() { 
        StackTrace stackTrace = new StackTrace(); 
        StackDepth = stackTrace.GetFrames().Length; 
        int threashold; 
    #if (VISUAL_STUDIO_12 && DEBUG) 
        threshold = MAXIMUM_STACK_DEPTH_FOR_VS12; // set to be a "safe" integer 
    #else 
        threshold = MAXIMUM_STACK_DEPTH; // the one in common use 
    #endif 
        if(StackDepth > threashold) { 
         throw new StackOverflowException("StackOverflow detected."); 
        } 
    } 
    

    凡constnat MAXIMUM_STACK_DEPTH_FOR_VS12是不會產生問題的手動發現的,數量最多。

    現在,我可以調試併發布應用程序,而無需更改任何內容,但仍喜歡聽取您的意見。

相關問題