Repository navigation
Avoid resolving HybridCache during Redis connection - #68127
Merged
Merged
Conversation
Co-authored-by: Copilot <[email protected]>
svick
marked this pull request as ready for review
July 31, 2026 13:10
Contributor
There was a problem hiding this comment.
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 viaIServiceProviderIsService.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; | ||
| } |
Member
Author
There was a problem hiding this comment.
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
approved these changes
Aug 12, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RedisCacheImpldetermines whether HybridCache is active by resolving theHybridCachesingleton while preparing a Redis connection. Only the presence of the registration is needed to add theHClibrary-name suffix.This change:
IServiceProviderIsServiceto inspect theHybridCacheregistration without constructing itIServiceProviderIsServiceHybridCachefactoryRelationship 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 secondHybridCacheinstance.The .NET 11 DI change in dotnet/runtime#124331 detects this re-entry and throws instead. In this specific connection path,
RedisCache.TryAddSuffixcatches that exception, so the duplicate instance is not created, but theHCsuffix is omitted.This PR removes the unnecessary re-entrant resolution from the component.
Addresses #67904.
Testing
GetService<HybridCache>()implementationNote
This pull request was created with the assistance of GitHub Copilot.