Using inside loops (foreach, etc)


#1

Long story short, I’m a Java developer who’s been thrown a C# project because “it’s kinda similar, right?” as we’ve lost our sole C# developer. Never done C# before. This is probably a fairly basic question, I expect it’s something fairly basic about variable scopes that’s probably not directly MiniProfiler related.

So.

I’m profiling a Razor template. It has several nested levels, roughly (pseudocode):

    var profiler = MiniProfiler.Current;
    var report = ViewBag.report as IEnumerable<Question>;

    using (profiler.Step("Parent template"))
    {       
        foreach (Question question in report)
        {
            using (profiler.Step("Question " + question.Id))
            {
                foreach (Question subquestion in question)
                {
                    using (profiler.Step("Subquestion " + subquestion.Id))
                    {
                       ....
                    }
                }
            }
        }
    }

Only the top level works. Not the nested ones. I tried redeclaring var profiler = MiniProfiler.Current; with different variable names in each level, to no avail.

How would I do it?


#2

After further testing, it works if I have an active breakpoint within that using block.
If I deactivate the breakpoint or remove it, and it doesn’t work. It does work if I give it the attribute to continue when hit, albeit this itself slows down execution but at least if gives me a scale of where the issues are.


#3

are you sure that the profiles are not recording? or just not showing up in the UI because the intervals are so small? There is a setting that by default will hideTrivial time profiles from the UI, which might be what is happening here.


#4

I would be surprised as I know the offending code was within one of these loops… Nevertheless it’s worth looking into, just in case. Thanks for the hint!