OrchardCMS/OrchardCore · error · InvalidOperationException

An implementation of 'LiquidTemplateContext' is required

Error message

An implementation of 'LiquidTemplateContext' is required

What it means

The `http_context` Liquid value resolves the current HttpContext by casting the TemplateContext to LiquidTemplateContext to access `ctx.Services` and `IHttpContextAccessor`. If evaluation happens with a plain Fluid TemplateContext, the cast fails and this InvalidOperationException is thrown before the accessor is even consulted.

Solutions

  1. Use LiquidTemplateContext (with the request-scoped IServiceProvider) when rendering any template that touches `http_context`.
  2. Render via ILiquidTemplateManager / the Liquid shape or view pipeline rather than raw Fluid APIs.
  3. For non-Liquid code, inject IHttpContextAccessor in C# instead of going through Liquid values.

Example fix

// before
var ctx = new TemplateContext();
await template.RenderAsync(ctx);
// after
var ctx = new LiquidTemplateContext(httpContext.RequestServices, memberAccessor);
await template.RenderAsync(ctx);
Defensive patterns

Strategy: type-guard

Validate before calling

if (context is not LiquidTemplateContext) throw new InvalidOperationException("http_context requires a LiquidTemplateContext with IHttpContextAccessor");

Type guard

bool IsLiquidContext(TemplateContext ctx) => ctx is LiquidTemplateContext;

Try / catch

try { await template.RenderAsync(ctx); } catch (InvalidOperationException ex) when (ex.Message.Contains("LiquidTemplateContext")) { /* construct LiquidTemplateContext with RequestServices */ }

Prevention

When it happens

Trigger: Accessing `{{ http_context }}` or its members (request, user, items, etc.) from a template rendered with a non-Liquid TemplateContext — custom render loops, tests, or direct FluidValue member invocation.

Common situations: Precompiling/testing Liquid templates with a hand-rolled context; rendering templates inside hosted services using the Fluid API directly; framework upgrades where custom code still creates the old base TemplateContext.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/44c95f097dc89ef3. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/HttpContextValue.cs:55

    {
        var httpContext = GetHttpContext(context);

        if (httpContext is null)
        {
            return new ValueTask<FluidValue>(NilValue.Instance);
        }

        return name switch
        {
            nameof(HttpContext.Items) => new ObjectValue(new HttpContextItemsWrapper(httpContext.Items)),
            _ => NilValue.Instance
        };
    }

    private static HttpContext GetHttpContext(TemplateContext context)
    {
        var ctx = context as LiquidTemplateContext
            ?? throw new InvalidOperationException($"An implementation of '{nameof(LiquidTemplateContext)}' is required");

        var httpContextAccessor = ctx.Services.GetRequiredService<IHttpContextAccessor>();

        return httpContextAccessor.HttpContext;
    }
}

View on GitHub (pinned to 4306c0717f)