如何修复 .NET 中的 StackOverFlowException
How to fix StackOverFlowException in .NET
ASP .NET 4.6 MVC 控制器在运行时创建 Razor 模板并使用 RazorEngine 运行它。
当从 Visual Studio 运行 运行 或从 Mono 运行时 Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute () is too complex.
时,一个大模板会导致堆栈溢出异常。
如果从模板中删除大量 div,它编译并运行正常。
如何解决这个问题?如何增加净堆栈大小或任何其他想法?
导致此问题的 Razor 模板有 2880 行。它包含 228 个变量和很多 div:
@inherits Reporting.ReportTemplateBase<MYApp.ViewModels.RazorViewModel>
<!DOCTYPE html>
<html>
....
<body>
@{
dynamic Vrapopref=ko.Keel;
dynamic Vymardada=Iif(Ko.Ymardus==1,2,Iif(Ko.Ymardus==2,0,1));
dynamic Vjag=Iif(Ko.Ymardus!=3,1,1000);
dynamic V4gr= true ;
dynamic Vs41gr=0;
dynamic Vs42gr=0;
...
dynamic _calculated229=Round(Vs52gr/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated229=SetDefault(_calculated229);
}
<div class='row' style='min-height:0.45cm'>
<div class='field' style='@TextBox(0.00,0.66,7.37,0.45);font-family:"Arial";font-weight:bold;'>@{try{WriteLiteral(Out(Eeva.Business.Prpalk.GetSfirmanimi()));} catch (Exception ex) {LogiVormiViga("Out(Eeva.Business.Prpalk.GetSfirmanimi())",ex);} }</div>
</div>
<div class='row' style='min-height:0.03cm'>
<div class='field' style='@TextBox(0.00,1.87,2.39,0.45);font-family:"Arial";font-size:9pt;'>@{try{WriteLiteral(Out(Ise.Regnr));} catch (Exception ex) {LogiVormiViga("Out(Ise.Regnr)",ex);} }</div>
</div>
<div class='row' style='min-height:0.42cm'>
<div class='field' style='left:0.66cm;font-family:"Arial";font-size:9pt;'>@Raw(IR("Reg nr"))</div>
</div>
<div class='row' style='min-height:0.71cm'>
<div class='field' style='@TextBox(0.00,0.66,7.74,0.55);font-family:"Arial";font-size:9pt;'>@{try{WriteLiteral(Out(RTrim(Ise.Tanav)+" "+RTrim(Ise.Piirkond)+" "+RTrim(Ise.Postiindek)));} catch (Exception ex) {LogiVormiViga("Out(RTrim(Ise.Tanav)+\" \"+RTrim(Ise.Piirkond)+\" \"+RTrim(Ise.Postiindek))",ex);} }</div>
</div>
<div class='row' style='min-height:0.71cm'>
<div class='field' style='left:0.66cm;font-family:"Arial";font-size:16pt;font-weight:bold;'>@Raw(IR("TULEMIARUANNE KUUDE KAUPA"))</div>
</div>
<div class='row' style='min-height:0.39cm'>
<div class='field' style='@TextBox(0.00,0.66,4.00,0.42);font-family:"Arial";font-size:9pt;font-weight:bold;'>@{try{WriteLiteral(Out(Format(Ko.Akuupaev,"-",Ko.Lkuupaev, "d")));} catch (Exception ex) {LogiVormiViga("Out(Format(Ko.Akuupaev,\"-\",Ko.Lkuupaev, \"d\"))",ex);} }</div>
...
</body>
</html>
整个模板在 http://wikisend.com/download/177922/Whosebug.TXT
结果:
.NET:
异常详情仅包含
{<Internal Error evaluating expression>}
单声道:
System.InvalidProgramException]: Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute () is too complex.
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run (RazorEngine.Templating.ExecuteContext context) <0x42194120 + 0x001b9> in <filename unknown>:0
at RazorEngine.Templating.TemplateService.Run (ITemplate template, RazorEngine.Templating.DynamicViewBag viewBag) <0x42193d10 + 0x0005b> in <filename unknown>:0
at (wrapper remoting-invoke-with-check) RazorEngine.Templating.TemplateService:Run (RazorEngine.Templating.ITemplate,RazorEngine.Templating.DynamicViewBag)
at RazorEngine.Templating.TemplateService.Parse (System.String razorTemplate, System.Object model, RazorEngine.Templating.DynamicViewBag viewBag, System.String cacheName) <0x4218ac70 + 0x00077> in <filename unknown>:0
at RazorEngine.Razor.Parse[T] (System.String razorTemplate, RazorEngine.T model, System.String cacheName) <0x4218ab00 + 0x0003f> in <filename unknown>:0
您的代码中有一个类似这样的循环 @do { } while (!endOfData);
确保 endOfData
在某个时候变为 true
。
此处已询问如何调试 Whosebug 异常:
- How to debug a Whosebugexception in .NET
基本上,
转到调试 -> 异常并选中 Common Language Runtime Exceptions
处的 thrown
复选框。现在,当您导致 Whosebug 异常时,调试器将停止。当发生这种情况时,忽略异常的堆栈跟踪并转到 Visual Studio's Call Stack window
并查看那里的调用堆栈。
这将指示生成无限递归的方法。
这是我一段时间以来看到的最丑陋的代码。您应该重构它以使用数组或将计算移至控制器。可能是 Mono 错误是正确的,页面 太复杂了。
dynamic _calculated21=Round(r.Total/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated21=SetDefault(_calculated21);
// about 200 similar lines snipped
dynamic _calculated229=Round(Vs52gr/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated229=SetDefault(_calculated229);
稍后:
_calculated21=SetDefault(_calculated217);
// about 200 identical lines removed
_calculated229=SetDefault(_calculated229);
ASP .NET 4.6 MVC 控制器在运行时创建 Razor 模板并使用 RazorEngine 运行它。
当从 Visual Studio 运行 运行 或从 Mono 运行时 Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute () is too complex.
时,一个大模板会导致堆栈溢出异常。
如果从模板中删除大量 div,它编译并运行正常。
如何解决这个问题?如何增加净堆栈大小或任何其他想法?
导致此问题的 Razor 模板有 2880 行。它包含 228 个变量和很多 div:
@inherits Reporting.ReportTemplateBase<MYApp.ViewModels.RazorViewModel>
<!DOCTYPE html>
<html>
....
<body>
@{
dynamic Vrapopref=ko.Keel;
dynamic Vymardada=Iif(Ko.Ymardus==1,2,Iif(Ko.Ymardus==2,0,1));
dynamic Vjag=Iif(Ko.Ymardus!=3,1,1000);
dynamic V4gr= true ;
dynamic Vs41gr=0;
dynamic Vs42gr=0;
...
dynamic _calculated229=Round(Vs52gr/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated229=SetDefault(_calculated229);
}
<div class='row' style='min-height:0.45cm'>
<div class='field' style='@TextBox(0.00,0.66,7.37,0.45);font-family:"Arial";font-weight:bold;'>@{try{WriteLiteral(Out(Eeva.Business.Prpalk.GetSfirmanimi()));} catch (Exception ex) {LogiVormiViga("Out(Eeva.Business.Prpalk.GetSfirmanimi())",ex);} }</div>
</div>
<div class='row' style='min-height:0.03cm'>
<div class='field' style='@TextBox(0.00,1.87,2.39,0.45);font-family:"Arial";font-size:9pt;'>@{try{WriteLiteral(Out(Ise.Regnr));} catch (Exception ex) {LogiVormiViga("Out(Ise.Regnr)",ex);} }</div>
</div>
<div class='row' style='min-height:0.42cm'>
<div class='field' style='left:0.66cm;font-family:"Arial";font-size:9pt;'>@Raw(IR("Reg nr"))</div>
</div>
<div class='row' style='min-height:0.71cm'>
<div class='field' style='@TextBox(0.00,0.66,7.74,0.55);font-family:"Arial";font-size:9pt;'>@{try{WriteLiteral(Out(RTrim(Ise.Tanav)+" "+RTrim(Ise.Piirkond)+" "+RTrim(Ise.Postiindek)));} catch (Exception ex) {LogiVormiViga("Out(RTrim(Ise.Tanav)+\" \"+RTrim(Ise.Piirkond)+\" \"+RTrim(Ise.Postiindek))",ex);} }</div>
</div>
<div class='row' style='min-height:0.71cm'>
<div class='field' style='left:0.66cm;font-family:"Arial";font-size:16pt;font-weight:bold;'>@Raw(IR("TULEMIARUANNE KUUDE KAUPA"))</div>
</div>
<div class='row' style='min-height:0.39cm'>
<div class='field' style='@TextBox(0.00,0.66,4.00,0.42);font-family:"Arial";font-size:9pt;font-weight:bold;'>@{try{WriteLiteral(Out(Format(Ko.Akuupaev,"-",Ko.Lkuupaev, "d")));} catch (Exception ex) {LogiVormiViga("Out(Format(Ko.Akuupaev,\"-\",Ko.Lkuupaev, \"d\"))",ex);} }</div>
...
</body>
</html>
整个模板在 http://wikisend.com/download/177922/Whosebug.TXT
结果:
.NET:
异常详情仅包含
{<Internal Error evaluating expression>}
单声道:
System.InvalidProgramException]: Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute () is too complex.
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run (RazorEngine.Templating.ExecuteContext context) <0x42194120 + 0x001b9> in <filename unknown>:0
at RazorEngine.Templating.TemplateService.Run (ITemplate template, RazorEngine.Templating.DynamicViewBag viewBag) <0x42193d10 + 0x0005b> in <filename unknown>:0
at (wrapper remoting-invoke-with-check) RazorEngine.Templating.TemplateService:Run (RazorEngine.Templating.ITemplate,RazorEngine.Templating.DynamicViewBag)
at RazorEngine.Templating.TemplateService.Parse (System.String razorTemplate, System.Object model, RazorEngine.Templating.DynamicViewBag viewBag, System.String cacheName) <0x4218ac70 + 0x00077> in <filename unknown>:0
at RazorEngine.Razor.Parse[T] (System.String razorTemplate, RazorEngine.T model, System.String cacheName) <0x4218ab00 + 0x0003f> in <filename unknown>:0
您的代码中有一个类似这样的循环 @do { } while (!endOfData);
确保 endOfData
在某个时候变为 true
。
此处已询问如何调试 Whosebug 异常:
- How to debug a Whosebugexception in .NET
基本上,
转到调试 -> 异常并选中 Common Language Runtime Exceptions
处的 thrown
复选框。现在,当您导致 Whosebug 异常时,调试器将停止。当发生这种情况时,忽略异常的堆栈跟踪并转到 Visual Studio's Call Stack window
并查看那里的调用堆栈。
这将指示生成无限递归的方法。
这是我一段时间以来看到的最丑陋的代码。您应该重构它以使用数组或将计算移至控制器。可能是 Mono 错误是正确的,页面 太复杂了。
dynamic _calculated21=Round(r.Total/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated21=SetDefault(_calculated21);
// about 200 similar lines snipped
dynamic _calculated229=Round(Vs52gr/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);
_calculated229=SetDefault(_calculated229);
稍后:
_calculated21=SetDefault(_calculated217);
// about 200 identical lines removed
_calculated229=SetDefault(_calculated229);