如何在.NET中修复StackOverFlowException

时间:2016-08-17 18:59:54

标签: c# .net asp.net-mvc razor razorengine

ASP .NET 4.6 MVC控制器在运行时创建Razor模板并使用RazorEngine运行它 从Visual Studio运行时,一个大模板会导致Stack Overflow异常,或者从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/stackoverflow.TXT

结果:

.NET:

enter image description here

异常详情仅包含

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

3 个答案:

答案 0 :(得分:0)

您的代码@do { } while (!endOfData);中有这样的循环确保endOfData在某个时刻变为true

答案 1 :(得分:0)

如何调试StackOverflow异常已在此处提出:

Bassically, 转到Debug - &gt;例外,然后选中thrown处的Common Language Runtime Exceptions复选框。现在,当您导致stackoverflow异常时,调试器将停止。当发生这种情况时,忽略异常的堆栈跟踪并转到Visual Studio's Call Stack window并查看那里的调用堆栈。

这将指示哪个方法正在生成无限递归。

答案 2 :(得分:0)

这是我见过一段时间最丑陋的代码。您应该重构它以使用数组或将计算移动到控制器。可能是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);