How to fix StackOverFlowException in .NET

2019-09-10 16:50发布

ASP .NET 4.6 MVC controller creates Razor Template at runtime and runs it using RazorEngine.
One big template causes Stack Overflow exception when running from Visual Studio or Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute () is too complex. when runing from Mono.

If large number of divs are removed from template, it compiles and runs OK.

How to fix this ? How to increase net stack size or any other idea ?

Razor template which causes this has 2880 lines. It contains 228 variables and lot of divs:

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

Whole template is in http://wikisend.com/download/177922/stackoverflow.TXT

Results:

.NET:

enter image description here

exception details contains only

{<Internal Error evaluating expression>}

Mono:

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条回答
可以哭但决不认输i
2楼-- · 2019-09-10 17:35

You have a loop like this in your code @do { } while (!endOfData); Make sure that endOfData becomes true at some point.

查看更多
Rolldiameter
3楼-- · 2019-09-10 17:37

That's some of the ugliest code I've seen for a while. You should refactor it to use arrays or move the calculations to the controller. It might be the Mono error is correct, that the page is too complex.

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);

And a bit later:

_calculated21=SetDefault(_calculated217);
// about 200 identical lines removed
_calculated229=SetDefault(_calculated229);
查看更多
相关推荐>>
4楼-- · 2019-09-10 17:41

How to debug a StackOverflow Exception has already been asked here:

Bassically, go to Debug -> exceptions and check the thrown checkbox at Common Language Runtime Exceptions. Now when you cause the stackoverflow exception, the debugger will stop. When that happens, ignore the exception's stacktrace and go to Visual Studio's Call Stack window and look at the call stack there.

That will indicate which method is generating the infinite recursion.

查看更多
登录 后发表回答