Razorlight: Child template just does not render in a couple of scenarios after .NET 2.0 upgrade

Created on 4 Jan 2018  路  35Comments  路  Source: toddams/RazorLight

Just upgraded to .NET 2.0 beta 1.
It is working, compiling, etc. But in a couple of scenarios, my child HTML template just does not render. In the generated HTML there's just nothing there, also no errors.

E.g. this used to work in 1.1, not anymore in 2.0:

<tr style="@(rs.ActivityBookings == null || rs.ActivityBookings.Count == 0 ? "display:inherit" : "display:none")">

And when using a foreach, I now have to check if there are more than zero items. Without that check, again nothing rendered.

This is quite frustrating as this is trial and error, with no clue why this is not working. Am I doing something wrong?

bug

Most helpful comment

@toddams When do you guess the nuget package will be released for this fix?

All 35 comments

  1. What is child HTML template and how it is rendered? Example?
  2. Did not get a thing about foreach. Example?

Thanks for the reply @toddams

  1. In the main template I've got:
<div class="stops">
@{ await IncludeAsync("/Booking/StopContainer.cshtml", Model);  }
</div>

StopContainer.cshtml is quite long. But it was working in .NET Core 1.1, but stopped working in .NET 2.0. It would just render <div class="stops"></div>

As soon as I stripped almost everything from StopContainer.cshtml, it would render something again.

But if I'd add something like the <div class="stops"></div> again

  1. For each:
foreach (var hb in rs.HotelBookings)
{
 <h1>@hb.Title</h1>
}

HotelBookings is a collection. When HotelBookings has zero items, above HTML causes again the template to not render.
I had to change it to:

@if (rs.HotelBookings.Count > 0) 
{
foreach (var hb in rs.HotelBookings)
{
 <h1>@hb.Title</h1>
}
}

It is quite weird, but very annoying. The child template is quite long, and time consuming to analyze.
I've stopped at the point where rendering a certain property causes the template not to render.

By the way, no intellisense or debug warnings.

@toddams when I compile the child template directly, it works fine.
It seems to have something to do with the rendering of child templates.

I've also have trouble with child template.
Snippet from main template:

 @foreach (var subscription in Model.Subscriptions)
            {
                <table class="outer" align="center" style="border-spacing: 0; font-family: arial, sans-serif; color: #333333; Margin: 0 auto; width: 100%; max-width: 600px;">

                    <tr>
                        <td class="one-column" style="padding-top: 0; padding-bottom: 0; padding-right: 0; padding-left: 0;">

                            <!-- Properties start -->
                            @{  await IncludeAsync("Parts.SubscriptionMail.Property.cshtml", subscription); }
                            <!-- Properties end -->
                        </td>
                    </tr>

Child template looks like this:

@using RazorLight
@using MyNamespace.TemplateModels.Subscription;
@using MyNamespace.TemplateModels;
@using DMyNamespace.TemplatePages;
@inherits SubscriptionTemplatePage<SubscriptionMailModel>

<table style="border-spacing: 0; font-family: arial, sans-serif; color: #333333; width: 100%;">
    @foreach (var property in Model.Properties)
    {
        <tr>
            <td class="contents" style="text-align: center; background-color: #fff; line-height: 24px; width: 100%; padding-top: 0; padding-bottom: 0; padding-right: 0; padding-left: 0;">
                <a href="http://test.com/@property.Id" style="color: #007272; text-decoration: none;">
                    <img src="@property.MainImage" width="100%" height="auto" style="border-width: 0;" />
                </a>
                <p style="Margin: 0; font-size: 14px; Margin-bottom: 10px;">&nbsp;</p>

                <a class="size-24" href="http://test.com/@property.Id" style="color: #007272; text-decoration: none; font-size: 24px;">
                    @Helper.GetHeader(property)
                </a><br />

                @Helper.GetSubHeader(property)<br />

                <strong>BOLIGTYPE:</strong>
                <span class="gray" style="color: #666666;">
                    @Helper.GetObjectType(property)
                </span><br />

                @Raw(@Helper.GetPrice(property))
                <p style="Margin: 0; font-size: 14px; Margin-bottom: 10px;">&nbsp;</p>

                <!--[if mso]>
                    <v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="http://test.com/@property.Id" arcsize="10%" stroke="f" fillcolor="#f87f00" style="height: 40px; v-text-anchor: middle; width: 200px;">
                    <w:anchorlock/>
                    <center>
                <![endif]-->
                <a class="btn-show-property" href="http://test.com/@property.Id" style="text-decoration: none; display: block; background-color: #f87f00; color: #ffffff; margin-top: 0; margin-bottom: 0; margin-right: auto; margin-left: auto; padding-top: 10px; padding-bottom: 10px; padding-right: 0; padding-left: 0; border-radius: 4px; font-size: 13px; font-weight: bold; width: 200px; font-family: arial, sans-serif;">Vis boligen</a>
                <!--[if mso]>
                    </center>
                    </v:roundrect>
                <![endif]-->
                <p style="Margin: 0; font-size: 14px; Margin-bottom: 10px;">&nbsp;</p>

            </td>
        </tr>
        <tr>
            <td class="row-padder" style="padding-top: 0; padding-bottom: 0; padding-right: 0; padding-left: 0; background-color: inherit;">&nbsp;</td>
        </tr>
    }
    </table>

This works when i have less then 5 objects in the subscription.Properties property. If i have more then 5 then it will stop rendering after <!-- Properties start -->. I don't get any exceptions but i get a broken template.

I found the bug, and that is a total disaster!)
Can't imagine how we've been using this for such a long time without facing this bug. It happens with big templates

Will prepare the next release, thank you for the very detailed description

@toddams When do you guess the nuget package will be released for this fix?

I also hit the same bug, child templates with a loop over a collection with the size more than 4 elements would not render at all.

@toddams: could you share details of the fix if you have one?

I am also seeing the same bug with child templates that contain loops over collections.

For me, @foreach (var int in new int[]{1, 2, 3, 4, 5, 6}) { <p>@int</p> } gives a blank template, but @foreach (var int in new int[]{1, 2, 3, 4, 5, 6}.Take(5)) { <p>@int</p> } displays fine. So it seems to be an issue with compiling an loop as soon as it goes over 5 elements

Do you have details on the fix?

I also have this issue. After playing around i saw that the problem (by me) only happens if i have a foreach loop within another foreach loop in a child view.

Can you please provide details of the fix?

Hi guys, i simply changed the file

    src\RazorLight\Internal\Buffering\ViewBuffer.cs  line21
        public static readonly int ViewPageSize = 512; //(from 256)

my template works fine, then change back to 256 , it's broken again.
after set it to 512, works ; then provide a large mumber items to generate, fail again,

I 'm sure that the ViewPageSize is the point. sth generated bursted the "buffer".

so i temporary set it to a big enough number.

@toddams @favishG

@AstralRoad It worked! Thanks a lot.

@AstralRoad
There is an issue with flushing the buffer after each partial iteration. Increasing the ViewPageSize will just move the threshold, but issue could occur again on larger templates. I'm working on a fix

Do you have a time plan for when this bugfix will be released?

@toddams
I used RazorLight - 2.0.0-beta1 in my open source project SmartCode. I also found such a serious bug.
When can you post a new version to nuge to fix this bug?
Thank you very much!!!

Commit 0dbc5e1d174411530c3ab81db94dfc3444dd4a4d does not fix rendering issue. It is still occurring for large templates. The problem is caused by MemoryPoolViewBufferScope implementation (clearing/passing/returning the buffer).
I implemented workaround here https://github.com/wik8wz/RazorLight/commit/35f06da97ec7da7677379932397560410a44d04b

Any progress on this? We have already invested a great deal of time on a large project and then ran into this issue once our template got to big.

We went live with the fork which I created. Might switch back to original one when fixed.

We went live with the fork which I created. Might switch back to original one when fixed.

@wik8wz Can you open a PR from your fork with the fix?

@AstralRoad
There is an issue with flushing the buffer after each partial iteration. Increasing the ViewPageSize will just move the threshold, but issue could occur again on larger templates. I'm working on a fix

@toddams could you please release a nuget beta package with that fix?

I hit what sounds like this bug today.... sorry for the formatting.. could not quite get i right.. so the point in the bottom HTML is missing as the error is not visible....

Fairly simple main view:

@using Microsoft.EntityFrameworkCore.Internal
@using RazorLight
@using TMServerDb.Models.Core
@inherits RazorLight.TemplatePage
@{
DisableEncoding = true;
}




@Model.Definition.Name


@{
await IncludeAsync("..\StandardItems\TournamentHeader.cshtml", (Model.Data as TMServerDb.Models.Core.MatchSet).Tournament);
}


@{
var ms = Model.Data as TMServerDb.Models.Core.MatchSet;
var grouped = ms.Matches.OrderBy(n => n.Number).ThenBy(p => p.Pitch.Name).GroupBy(s => s.PlannedStart);

    foreach (IGrouping<DateTime, Match> grouping in grouped)
    {
        await IncludeAsync("..\\StandardItems\\MatchGrouping.cshtml", grouping);

        <br/>
    }
}


The nested view:

@using RazorLight
@using TMServerDb.Models.Core
@inherits RazorLight.TemplatePage>















@{
foreach (Match match in Model.OrderBy(x => x.Pitch.Name))
{





}
}

@Model.Key.Date.ToShortDateString()@Model.Key.ToShortTimeString()
#PitchClass
@match.Number@match.Pitch.Name@match.Class.Name

When rendering, in my test, I have 30 odd groupings... but only a few make it to the pdf... intercepting the result from the rendering, explains why... notice the numerous ending of html and body tags...





Tournament Schedule

Amsterdam Open 2019

Uithoorn

01-06-2019 - 02-06-2019

<br>














































01-06-2019 09:00
# Pitch Class
1 Pitch 1 4th Division
2 Pitch 2 1st Division
3 Pitch 3 Women Division
4 Pitch 4 Women Division
5 Pitch 5 2nd Division
6 Pitch 6 3th Division















































01-06-2019 09:30
# Pitch Class
7 Pitch 1 4th Division
8 Pitch 2 2nd Division
9 Pitch 3 1st Division
10 Pitch 4 2nd Division
11 Pitch 5 1st Division
12 Pitch 6 3th Division















































01-06-2019 10:00
# Pitch Class
13 Pitch 1 4th Division
14 Pitch 2 Women Division
15 Pitch 3 2nd Division
16 Pitch 4 2nd Division
17 Pitch 5 1st Division
18 Pitch 6 3th Division















































01-06-2019 10:30
# Pitch Class
19 Pitch 1 4th Division
20 Pitch 2 1st Division
21 Pitch 3 Women Division
22 Pitch 4 1st Division
23 Pitch 5 2nd Division
24 Pitch 6 3th Division























@wik8wz , is your NoPoolingViewBufferScope work fine in production? Have you noticed any issues like high memory consumption or memory leak?

@wik8wz , is your NoPoolingViewBufferScope work fine in production? Have you noticed any issues like high memory consumption or memory leak?

Nothing was reported until now. We do recycle our app pool everyday anyway.
Our service is not very loaded at the moment. We are sending ~250 rendered email per month right now

Big thanks to wik8wz for fixing this issue. We are using your branch and no longer have the partial view issues that we were facing. Another interesting thing we found was that you need to be careful about making var assignments inside of the partial views, you are better served to just directly looking into your model for all of your data.

@toddams , are you ok with https://github.com/wik8wz/RazorLight/commit/35f06da97ec7da7677379932397560410a44d04b implementation? Will you accept PR with it? We are also using his branch in a couple of projects without any issues.

@markleacompassphs in meantime we usage of service in production increased significantly to 1000s emails per month. Did not face rendering issue again since fix was implemented (over 8 months now). Quite happy how it is working to be honest.

I recently requested write access to this repo form @toddams in order to help out with the project and get a few key bugs fixed.

@wik8wz Do you have any interest in a pull request for your fix? I'd rather you formally submit one than me copy-paste your contribution, on legal grounds.

@jzabroski I created one here: https://github.com/toddams/RazorLight/pull/275 Sadly I'm no longer working with C# and therefore will no longer support development here. I hope this works for you (I saw there are no merge conflicts)

@wik8wz Why sadly? Friend request me on LinkedIn, who knows, maybe if you love C# I would love to work with you.

Thanks for your help!

Adding my note here that @wik8wz 's fork seems to have fixed the problem for me. Much thanks!

@toddams is there any chance we can get this fix integrated? Or at least add a disclaimer to the README on the repo? I spent the better part of the day tracking down a solution. Was afraid I'd have to switch to an entirely different Razor rendering library.

I'm waiting on @toddams to give me access to update the Nuget package, then within a few days I should be able to fix all these problems. He gave me write access to the repo, but not the nuget feed.

@jzabroski That's great news - we are still having great success with this branch, in fact we are getting ready to deploy our new communication service (replacing a horribly done WCF from years back) with the new .net core/Razor Light that has a pretty slick data augmentation architecture. Keep us posted!

Duplicate: #261

@markleacompassphs Can you please confirm if the latest 2.0.0-beta2 release fixes this problem? I feel pretty confident it does.

@markleacompassphs Just a nudge - I believe this is fixed. Looking for someone else to confirm.

@jzabroski we are not having any issues with the partial views with big templates ever since we switched to using the branch that @wik8wz created a long time ago. We have just been using his source code in our solution we have it as a project and we can debug as needed. We did have an issue with the cache yesterday, but so far after a rebuild and re-deploy it appears to be working fine. We are scheduled to go into Production this weekend.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdrianPell picture AdrianPell  路  4Comments

IInvocation picture IInvocation  路  6Comments

diegobrum picture diegobrum  路  5Comments

tresoldigiorgio picture tresoldigiorgio  路  4Comments

btran021111 picture btran021111  路  4Comments