Skip to content

Register core output caching services in Redis output cache integration - #21222

Merged
Ankit Jain (radical) merged 1 commit into
microsoft:mainfrom
curllog:fix/redis-outputcache-net11
Oct 30, 2026
Merged

Ankit Jain (radical) merged 1 commit into
microsoft:mainfrom
curllog:fix/redis-outputcache-net11

Conversation

@curllog

@curllog Curllog (curllog) commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Register core output caching services in Redis output cache integration

Fixes #21221

Problem

On .NET 11, an app that uses Aspire's Redis output caching together with app.UseOutputCache() crashes at startup:

System.InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.OutputCaching.IOutputCachePolicyProvider' while attempting to activate
'Microsoft.AspNetCore.OutputCaching.OutputCacheMiddleware'.

This affects the Aspire Starter template's Web frontend as soon as it targets net11.0.

AddRedisOutputCache, AddKeyedRedisOutputCache and AddRedisClientBuilder(...).WithOutputCache() only registered the Redis IOutputCacheStore (through AddStackExchangeRedisOutputCache). They never called AddOutputCache(). In .NET 11, OutputCacheMiddleware requires IOutputCachePolicyProvider, which only AddOutputCache() registers, so the middleware can't be created.

Fix

AddRedisOutputCacheCore, which all three public APIs call, now calls builder.Services.AddOutputCache() before registering the Redis store.

  • Calling it twice is harmless: AddOutputCache() uses TryAdd*, so apps that already call it themselves are unaffected.
  • The Redis store is still used: AddOutputCache() registers its in-memory IOutputCacheStore with TryAdd, so it doesn't replace the Redis store. The existing AddsRedis*OutputCacheCorrectly tests still pass and check this.
  • Works on .NET 8, 9 and 10: AddOutputCache() exists on all supported TFMs. No public API changes.

Tests

I added RegistersOutputCachePolicyProvider to AspireRedisOutputCacheExtensionsTests.cs, a [Theory] that covers all three entry points (plain, keyed, builder). Each case checks that IOutputCachePolicyProvider is registered.

The test is wrapped in #if NET11_0_OR_GREATER, because IOutputCachePolicyProvider doesn't exist before .NET 11. The repo already uses the same pattern elsewhere, e.g. tests/Shared/Processes/ProcessTestFixture.cs. It doesn't need Redis or Docker; it only checks DI registrations.

Before the fix (net11.0):

failed RegistersOutputCachePolicyProvider(api: "builder")
  Assert.NotNull() Failure: Value is null
failed RegistersOutputCachePolicyProvider(api: "plain")
  Assert.NotNull() Failure: Value is null
failed RegistersOutputCachePolicyProvider(api: "keyed")
  Assert.NotNull() Failure: Value is null
  total: 3, failed: 3, succeeded: 0

After the fix (net11.0):

  total: 3, failed: 0, succeeded: 3

How to test

  1. Run the new tests on .NET 11:
    dotnet test tests/Aspire.StackExchange.Redis.OutputCaching.Tests -f net11.0 --filter-method "*OutputCachePolicyProvider*"
    
    Expected: 3 passed.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21222

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21222"

@github-actions github-actions Bot added the area-integrations Issues pertaining to Aspire Integrations packages label Oct 20, 2026
@curllog

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@radical
Ankit Jain (radical) enabled auto-merge (squash) October 30, 2026 21:35
@radical
Ankit Jain (radical) merged commit 57971d4 into microsoft:main Oct 30, 2026
752 of 764 checks passed
@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 17.0 milestone Oct 30, 2026
@curllog
Curllog (curllog) deleted the fix/redis-outputcache-net11 branch October 31, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-integrations Issues pertaining to Aspire Integrations packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddRedisOutputCache crashes at startup on .NET 11: unable to resolve IOutputCachePolicyProvider

2 participants