Support `IOptionsMonitor<SqlServerCacheOptions>` for runtime configuration updates
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
## Description
`Microsoft.Extensions.Caching.SqlServer` currently relies on `IOptions`, which effectively makes its configuration immutable after the service has been constructed.
This becomes a limitation in environments where the SQL Server connection string can change at runtime, such as:
- Secret rotation (Azure Key Vault, CyberArk, HashiCorp Vault, etc.)
- Custom `IConfigurationProvider`s that support reloads
- Dynamic multi-environment or failover scenarios
Although the ASP.NET Core configuration system supports runtime configuration reloads through `IOptionsMonitor`, `SqlServerCache` does not take advantage of this capability. As a result, the cache continues using the original connection string until the application is restarted.
This forces applications to restart in order to pick up a new database connection string, even though the rest of the configuration pipeline is already reactive.
## Proposed solution
Replace the dependency on `IOptions` with `IOptionsMonitor`.
`SqlServerCache` could subscribe to configuration changes using `OnChange()` and recreate its internal database access components whenever the monitored options change.
A possible implementation would:
- Inject `IOptionsMonitor`
- Initialize internal state from `CurrentValue`
- Subscribe to `OnChange()`
- Atomically replace the internal `DatabaseOperations` instance (for example via `Interlocked.Exchange`) whenever the options change
- Dispose the `OnChange()` registration when the cache is disposed
## Benefits
- Enable seamless connection string rotation without application restarts.
- Align `SqlServerCache` with the rest of the ASP.NET Core options ecosystem.
- Remain fully backward compatible for existing applications.
- Benefit users relying on configuration providers that support reload-on-change.
- Require only minimal implementation changes while preserving thread safety, since `DatabaseOperations` can be atomically replaced.
Contributor guide
Research direction
The issue names Microsoft.Extensions.Caching.SqlServer's SqlServerCache and DatabaseOperations; start by locating those entry points and tracing the current IOptions lifetime. Check the configuration-change and disposal paths, then verify that a changed connection string is used without an application restart and that the change registration is disposed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100