一个*Exception上System.Diagnotics.StackTrace()
问题描述:
我一直在写针对.NET框架V3.5和Visual Studio的Web应用程序会出现2013年一个*Exception上System.Diagnotics.StackTrace()
间接递归在它someties造成*Exception我这么写一种检查堆栈溢出的方法。
public static void Check*() {
StackTrace stackTrace = new StackTrace();
StackDepth = stackTrace.GetFrames().Length;
if(StackDepth > MAXIMUM_STACK_DEPTH) {
throw new *Exception("* detected.");
}
}
的问题是,一个*Exception在第一行中出现,即new StackTrace()
,所以我不能照顾它。
我知道调用StackTrace()也会将堆栈加深几级,所以我明白这可能会发生。然而,有一些耐人寻味:
- 选择了在Visual Studio 2012 的Visual Studio(ASP.NET)开发服务器(以下卡西尼)有没有问题,所以我的IIS设置或类似的东西是疑似。
- 堆栈在发生异常时并不够深。
这只发生在调试。不管配置如何(即调试/发布)。
编辑:我试图changed IIS Express settings和它并没有差异。此外,尝试本地IIS选项也没有运气,无论是。所以,
if(RunningWithVisualStudio) { // Start Debugging or Without Debugging
if(UsingCassini) {
throw new StackOrverflowException("A catchable exception."); // expected
} else {
throw new *Exception("I cannot catch this dang exception.");
}
} else { // publish on the identical ApplicationPool.
throw new StackOrverflowException("A catchable exception."); // expected
}
我想我犯的错误配置IIS快递但我现在完全失去了。
答
下面是我做的事是解决方法:
- 下面我加入的.csproj文件来定义IDE的当前版本。
- Defined DEBUG constant
-
加入使用预处理器指令的条件。
public static void Check*() { 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 *Exception("* detected."); } }
凡constnat MAXIMUM_STACK_DEPTH_FOR_VS12是不会产生问题的手动发现的,数量最多。
现在,我可以调试并发布应用程序,而无需更改任何内容,但仍喜欢听取您的意见。