如何修复.NET中的StackOverFlowException [英] How to fix StackOverFlowException in .NET

查看:99
本文介绍了如何修复.NET中的StackOverFlowException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP .NET 4.6 MVC控制器在运行时创建Razor模板,并使用RazorEngine运行它.
从Visual Studio运行时,一个大模板会导致堆栈溢出异常.从Mono运行时, Method CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute()太复杂..

如果从模板中删除了大量的div,它将编译并运行正常.

如何解决此问题?如何增加净堆栈大小或其他任何想法?

导致此问题的剃刀模板有2880行.它包含228个变量和很多div:

  @inherits Reporting.ReportTemplateBase< MYApp.ViewModels.RazorViewModel><!DOCTYPE html>< html>....<身体>@ {动态Vrapopref = ko.Keel;动态Vymardada = Iif(Ko.Ymardus == 1,2,Iif(Ko.Ymardus == 2,0,1));动态Vjag = Iif(Ko.Ymardus!= 3,1,1000);动态V4gr = true;动态Vs41gr = 0;动态Vs42gr = 0;...动态_calculated229 = Round(Vs52gr/Vjag * Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);_calculated229 = SetDefault(_calculated229);}< div class ='row'style ='最小高度: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 ='左:0.66cm; font-family:"Arial"; font-size:16pt; font-weight:bold;'> @Raw(IR("TULEMIARUANNE KUUDE KAUPA"))</div></div>< div class ='row'style ='最小高度: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(例外){LogiVormiViga("Out(Format(Ko.Akuupaev,\"-\,Ko.Lkuupaev,\"d \")),ex);}}</div>...</body></html> 

整个模板位于

例外详细信息仅包含

  {<内部错误评估表达式>} 

单声道:

  System.InvalidProgramException]:方法CompiledRazorTemplates.Dynamic.dbcfcaeeb:Execute()太复杂.在RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run处(RazorEngine.Templating.ExecuteContext上下文)< 0x42194120 + 0x001b9>在<未知的文件名>:0中< 0x42193d10 + 0x0005b>在RazorEngine.Templating.TemplateService.Run(ITemplate模板,RazorEngine.Templating.DynamicViewBag viewBag)处.在<文件名未知>:0中在(包装器远程调用检查)RazorEngine.Templating.TemplateService:Run(RazorEngine.Templating.ITemplate,RazorEngine.Templating.DynamicViewBag)< 0x4218ac70 + 0x00077>在RazorEngine.Templating.TemplateService.Parse(System.String razorTemplate,System.Object模型,RazorEngine.Templating.DynamicViewBag viewBag,System.String cacheName)处.在<文件名未知>:0中在RazorEngine.Razor.Parse [T]处(System.String razorTemplate,RazorEngine.T模型,System.String cacheName)< 0x4218ab00 + 0x0003f>在<文件名未知>:0中 

解决方案

这是一段时间以来我看到的最丑陋的代码.您应该重构它以使用数组或将计算结果移动到控制器.页面 太复杂了,这可能是Mono错误是正确的.

  dynamic _calculated21 = Round(r.Total/Vjag*Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);_calculated21 = SetDefault(_calculated21);//大约有200条相似的行动态_calculated229 = Round(Vs52gr/Vjag * Iif(Core.Left(r.BilskeemKontoklass,1)=="K",-1,1),Vymardada);_calculated229 = SetDefault(_calculated229); 

再过一会儿:

  _calculated21 = SetDefault(_calculated217);//删除约200条相同的行_calculated229 = SetDefault(_calculated229); 

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:

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 

解决方案

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

这篇关于如何修复.NET中的StackOverFlowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆