Skip to content

Output Caching: Correctly gate auto-registration of UseOutputCache() middleware#22897

Merged
AndyButland merged 1 commit into
release/17.4.1from
v17/bugfix/handle-existing-use-output-cache-17.4.1
May 19, 2026
Merged

Output Caching: Correctly gate auto-registration of UseOutputCache() middleware#22897
AndyButland merged 1 commit into
release/17.4.1from
v17/bugfix/handle-existing-use-output-cache-17.4.1

Conversation

@AndyButland
Copy link
Copy Markdown
Contributor

@AndyButland AndyButland commented May 19, 2026

Description

In 17.4 we centralised the registration of the ASP.NET Core output cache middleware in UmbracoApplicationBuilder.RegisterDefaultRequiredMiddleware(), gated on the presence of IOutputCacheStore in the container. That gate is too broad — IOutputCacheStore is registered by any call to services.AddOutputCache(...), including calls made by the consuming application for its own (non-Umbraco) output caching needs.

The result: applications that had their own services.AddOutputCache(...) + app.UseOutputCache() in Program.cs (working fine pre-17.4) now get a second UseOutputCache() registration added by Umbraco, and requests fail with:

InvalidOperationException: Another instance of OutputCacheFeature already exists.
Only one instance of OutputCacheMiddleware can be configured for an application.

Fix

Replace the IOutputCacheStore gate with a dedicated marker, IUmbracoManagedOutputCacheMarker, that is registered only when Umbraco itself enables output caching — via Umbraco:CMS:Website:OutputCache:Enabled or Umbraco:CMS:DeliveryApi:OutputCache:Enabled. Applications calling services.AddOutputCache(...) for their own purposes are no longer affected by Umbraco's automatic middleware registration.

How to test

Reproduces with the UmbracoNewsDashboard sample project on 17.4.0:

  1. services.AddOutputCache(...) + app.UseOutputCache() in Program.cs, no Umbraco output cache settings enabled → InvalidOperationException on every request before this fix.
  2. After this fix → caching works, no exception, no behaviour change.

…braco managed caching via configuration is enabled, and not consider existing implementation specific registrations.
Copilot AI review requested due to automatic review settings May 19, 2026 12:58
@claude
Copy link
Copy Markdown

claude Bot commented May 19, 2026

Claude finished @AndyButland's task in 1m 52s —— View job


PR Review

Target: origin/release/17.4.1 · Based on commit: 6ce7c8c9

Introduces IUmbracoManagedOutputCacheMarker (Core layer) to gate Umbraco's automatic UseOutputCache() middleware registration on its own caching being configured, rather than on the presence of IOutputCacheStore which any application-level AddOutputCache(...) call would also register.

  • Modified public API: Added IUmbracoManagedOutputCacheMarker (interface) and UmbracoManagedOutputCacheMarker (class) to Umbraco.Cms.Core.DependencyInjection
  • Breaking changes: None — purely additive public API, internal gate condition changed

Suggestions

  • src/Umbraco.Core/DependencyInjection/IUmbracoManagedOutputCacheMarker.cs:1: Both the interface and concrete class live in the same file. This matches the existing IBackOfficeEnabledMarker convention exactly, so no action needed — just noting the intentional deviation from "one class per file" for reviewers.

Approved

The fix is correct and tightly scoped. The new marker pattern is a direct analogue of IBackOfficeEnabledMarker (same file, same structure, same namespace), so it's idiomatic for this codebase. TryAddSingleton is the right choice — it prevents double-registration when both Website and Delivery API caching are enabled simultaneously, and the first registration wins (which is fine since the marker carries no state). The old IOutputCacheStore gate was a legitimate bug: any application calling services.AddOutputCache(...) for its own purposes would have inadvertently triggered Umbraco's UseOutputCache() registration. No breaking changes detected.

Labels applied: area/backend, category/api

@AndyButland AndyButland changed the title Output Caching: Correctly gate auto-registration of UseOutputCache() middleware Output Caching: Correctly gate auto-registration of UseOutputCache() middleware May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts Umbraco’s automatic UseOutputCache() middleware registration so it only happens when Umbraco itself has enabled output caching (via Website template caching or Delivery API caching), avoiding unintended middleware registration when an application calls services.AddOutputCache(...) for its own purposes.

Changes:

  • Introduces an IUmbracoManagedOutputCacheMarker service as the explicit signal that Umbraco-managed output caching is enabled.
  • Registers the marker when Website and/or Delivery API output caching is configured.
  • Updates UmbracoApplicationBuilder to gate UseOutputCache() on the marker instead of IOutputCacheStore.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/Umbraco.Web.Website/DependencyInjection/UmbracoBuilderExtensions.cs Registers the marker when Website output caching is enabled.
src/Umbraco.Web.Common/ApplicationBuilder/UmbracoApplicationBuilder.cs Switches middleware auto-registration gate from IOutputCacheStore to the new marker.
src/Umbraco.Core/DependencyInjection/IUmbracoManagedOutputCacheMarker.cs Adds the marker interface + implementation used to signal Umbraco-managed output caching.
src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs Registers the marker when Delivery API output caching is enabled.

Copy link
Copy Markdown
Contributor

@NillasKA NillasKA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and tests good to me!

@AndyButland AndyButland merged commit 336bffe into release/17.4.1 May 19, 2026
27 checks passed
@AndyButland AndyButland deleted the v17/bugfix/handle-existing-use-output-cache-17.4.1 branch May 19, 2026 14:18
AndyButland added a commit that referenced this pull request May 19, 2026
…` middleware (#22897)

Correct the gating of the call to UseOutputCache() to only proceed Umbraco managed caching via configuration is enabled, and not consider existing implementation specific registrations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants