Skip to content

Avoid resolving HybridCache during Redis connection - #68127

Merged
BrennanConroy merged 1 commit into
dotnet:mainfrom
svick:redis-hybridcache-service-check
Aug 12, 2026
Merged

BrennanConroy merged 1 commit into
dotnet:mainfrom
svick:redis-hybridcache-service-check

Conversation

@svick

@svick svick commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Summary

RedisCacheImpl determines whether HybridCache is active by resolving the HybridCache singleton while preparing a Redis connection. Only the presence of the registration is needed to add the HC library-name suffix.

This change:

  • uses IServiceProviderIsService to inspect the HybridCache registration without constructing it
  • keeps the capability optional for compatibility with containers that do not provide IServiceProviderIsService
  • updates the existing test to prove that detection does not invoke the registered HybridCache factory

Relationship to dotnet/runtime#124331

The functional problem reported in #67904 occurs with the .NET 10 DI implementation. With a synchronously completed ConnectionMultiplexerFactory, such as the factory used by Aspire Redis distributed caching, connection preparation can re-enter singleton resolution and publish a second HybridCache instance.

The .NET 11 DI change in dotnet/runtime#124331 detects this re-entry and throws instead. In this specific connection path, RedisCache.TryAddSuffix catches that exception, so the duplicate instance is not created, but the HC suffix is omitted.

This PR removes the unnecessary re-entrant resolution from the component.

Addresses #67904.

Testing

  • negative control: the HybridCache detection test fails on both targets with the previous GetService<HybridCache>() implementation

Note

This pull request was created with the assistance of GitHub Copilot.

@gfoidl gfoidl added feature-caching Includes: StackExchangeRedis and SqlServer distributed caches area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares labels Jul 31, 2026
@gfoidl
gfoidl temporarily deployed to copilot-pat-pool July 31, 2026 12:03 — with GitHub Actions Inactive
@gfoidl
gfoidl temporarily deployed to copilot-pat-pool July 31, 2026 12:04 — with GitHub Actions Inactive
@svick
svick marked this pull request as ready for review July 31, 2026 13:10
@svick
svick requested a review from halter73 as a code owner July 31, 2026 13:10
Copilot AI review requested due to automatic review settings July 31, 2026 13:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates RedisCacheImpl to detect whether HybridCache is registered without resolving/constructing it during Redis connection preparation, avoiding re-entrant singleton resolution while still enabling the HC library-name suffix.

Changes:

  • Switch HybridCache detection from GetService<HybridCache>() to a registration check via IServiceProviderIsService.IsService.
  • Keep the behavior container-tolerant by making the detection dependency optional.
  • Update the existing test to prove HybridCache detection does not invoke the registered HybridCache factory.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Caching/StackExchangeRedis/src/RedisCacheImpl.cs Changes HybridCache detection to a non-constructing registration check used by RedisCache.TryAddSuffix.
src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs Updates the detection test to fail if HybridCache is ever resolved during detection.

Comment on lines +14 to 29
private readonly IServiceProviderIsService? _serviceProviderIsService;

internal override bool IsHybridCacheActive()
=> _services.GetService<HybridCache>() is not null;
=> _serviceProviderIsService?.IsService(typeof(HybridCache)) == true;

public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, ILogger<RedisCache> logger, IServiceProvider services)
public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, ILogger<RedisCache> logger, IServiceProviderIsService? serviceProviderIsService = null)
: base(optionsAccessor, logger)
{
_services = services; // important: do not check for HybridCache here due to dependency - creates a cycle
_serviceProviderIsService = serviceProviderIsService;
}

public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, IServiceProvider services)
public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, IServiceProviderIsService? serviceProviderIsService = null)
: base(optionsAccessor)
{
_services = services; // important: do not check for HybridCache here due to dependency - creates a cycle
_serviceProviderIsService = serviceProviderIsService;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think casting IServiceProvider to IServiceProviderIsService is more compatible than injecting optional IServiceProviderIsService. If a container doesn't support IServiceProviderIsService, this only means the "HC" suffix is not used and I think that's acceptable.

@BrennanConroy
BrennanConroy merged commit 78cf766 into dotnet:main Aug 12, 2026
36 of 40 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 13, 2026
@svick
svick deleted the redis-hybridcache-service-check branch August 24, 2026 15:45

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — f9762d2c Deployed Jul 31, 2026 by gfoidl via conclusion #772
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares feature-caching Includes: StackExchangeRedis and SqlServer distributed caches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants