Skip to content

Using Handlebars.Net in a Blazor WASM throws error when using ".Compile()" #345

Closed

Description

@StefH

This is because the WASM linker removes some of the "Expression" code. For a solution, see zzzprojects/System.Linq.Dynamic.Core#360

Or I can also make a PR for this project.

Full Exception:

HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Reflection.Emit.ILGenerator.Emit (System.Reflection.Emit.OpCode opcode, System.Reflection.ConstructorInfo con) <0x2334fc0 + 0x00042> in :0 at System.Linq.Expressions.Compiler.CompilerScope.EmitNewHoistedLocals (System.Linq.Expressions.Compiler.LambdaCompiler lc) <0x231a968 + 0x0024e> in :0 at System.Linq.Expressions.Compiler.CompilerScope.Enter (System.Linq.Expressions.Compiler.LambdaCompiler lc, System.Linq.Expressions.Compiler.CompilerScope parent) <0x2307628 + 0x00048> in :0 at System.Linq.Expressions.Compiler.LambdaCompiler.EmitLambdaBody (System.Linq.Expressions.Compiler.CompilerScope parent, System.Boolean inlined, System.Linq.Expressions.Compiler.LambdaCompiler+CompilationFlags flags) <0x2307180 + 0x00012> in :0 at System.Linq.Expressions.Compiler.LambdaCompiler.EmitLambdaBody () <0x2306978 + 0x00034> in :0 at System.Linq.Expressions.Compiler.LambdaCompiler.Compile (System.Linq.Expressions.LambdaExpression lambda) <0x228edf8 + 0x00026> in :0 at System.Linq.Expressions.Expression1[TDelegate].Compile (System.Boolean preferInterpretation) <0x228ea98 + 0x00002> in <filename unknown>:0 at System.Linq.Expressions.Expression1[TDelegate].Compile () <0x228e9e0 + 0x00004> in :0 at HandlebarsDotNet.Compiler.FunctionBuilder.Compile (System.Collections.Generic.IEnumerable1[T] expressions, System.String templatePath) <0x2227300 + 0x0001c> in <filename unknown>:0 --- End of inner exception stack trace --- at HandlebarsDotNet.Compiler.FunctionBuilder.Compile (System.Collections.Generic.IEnumerable1[T] expressions, System.String templatePath) <0x2227300 + 0x0003e> in :0 at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile (System.IO.TextReader source) <0x21dd428 + 0x00048> in :0 at HandlebarsDotNet.Handlebars+HandlebarsEnvironment.Compile (System.IO.TextReader template) <0x21dd1e8 + 0x0000e> in :0 at HandlebarsDotNet.Handlebars+HandlebarsEnvironment.Compile (System.String template) <0x21dd008 + 0x00026> in :0 at BlazorApp1.Pages.Index.OnInitialized () <0x212a288 + 0x001d0> in :0

Activity

  1. added this to the v3 milestone on Dec 22, 2021
  2. rexm commented on Nov 22, 2026

    @rexm
    Member

    /harness adopt

    Start by contemplating the relevance of Blazor WASM as of today late 2026

  3. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    Contributor

    I'm the agent responsible for managing intake of issues on this repo, acting under @rexm's steering. I understand the request as: first assess how relevant Blazor WebAssembly (and its trimming/AOT/runtime constraints) still is in late 2026, then determine whether Handlebars.Net's Compile() still fails there and what, if anything, should change. I'll evaluate whether I can address this autonomously.

  4. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    Contributor

    Assessment: Blazor WASM relevance (late 2026)

    Blazor WebAssembly is still relevant. It's a supported, shipping hosting model, and the "Auto" render mode in Blazor Web Apps runs components in WASM once the client runtime has downloaded. What has changed since 2020 is how Expression.Compile() behaves there. On browser-wasm (and on iOS/tvOS/Mac Catalyst and NativeAOT), System.Linq.Expressions no longer emits IL; it always runs the expression interpreter. The failure in this issue was a NullReferenceException inside ILGenerator.Emit after the old Mono linker had stripped Reflection.Emit, and that code path doesn't exist on those runtimes anymore. So the original crash is obsolete. The question that still matters is whether Handlebars.Net renders correctly when expressions are interpreted.

    What I checked

    I temporarily forced the library's default compiler to interpret (Compile(preferInterpretation: true)) and ran the full test suite on net10.0:

    • Rendering is correct. Every test passed except the ones below.
    • Recursion-limit tests stack-overflow. PartialTests.RecursionUnboundedPartial, PartialTests.RecursionBoundedAboveLimitPartial and InlinePartialTests.RecursionBoundedAboveLimitInlinePartial crash the process before the default PartialRecursionDepthLimit (100) throws HandlebarsRuntimeException, because interpreted frames are much deeper. On WASM/AOT, a runaway recursive partial would therefore crash instead of throwing a catchable error. Lowering the default would change behavior for everyone, so that's your call. Options:
      • (a) Leave it as is and document "set PartialRecursionDepthLimit lower on interpreted runtimes".
      • (b) Pick a lower default only when RuntimeFeature.IsDynamicCodeCompiled is false.
    • One test fails for a reason that isn't a library bug. BasicIntegrationTests.TestNoWhitespaceBetweenExpressions fails only because its helper compares objects with ==. The compiled path happens to intern string literals via ldstr, and the interpreter doesn't.
    • A public setting is ignored. CompileTimeConfiguration.ExpressionCompiler is settable, but nothing reads it. The working way to plug in a custom compiler is an IFeature that sets ICompiledHandlebarsConfiguration.ExpressionCompiler.

    What changed

    • Handlebars-Net/Handlebars.Net: test: cover rendering with interpreted expression trees (Blazor WASM / iOS / NativeAOT) #698 is test-only, with no library or API change. It adds ExpressionInterpretationTests, which renders 17 representative templates (helpers, block helpers, each/with/if/unless, @root/../, lookup, subexpressions, partials, partial blocks, inline partials) through an interpreting expression compiler. It asserts output identical to the compiled path, and also covers compile-error parity. Full suite: 2344 passed. It's MERGEABLE and set to auto-merge when checks pass. It references this issue without closing it.

    Not done / needs a decision

    • I couldn't run an actual Blazor WASM app in a browser. Interpreter-mode tests are the proxy for it.
    • I didn't change the recursion-depth behavior, the unused ExpressionCompiler property, or the issue's open state. My recommendation: close this issue as resolved by the platform once test: cover rendering with interpreted expression trees (Blazor WASM / iOS / NativeAOT) #698 lands, and open separate issues for the recursion-depth crash (option b if you want a fix; that would be a patch release) and for the ignored ExpressionCompiler property. Let me know and I'll take either of them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions