Add mechanism to clear ServiceProviderCache (#27189)#37487
Add mechanism to clear ServiceProviderCache (#27189)#37487AndriySvyryd merged 8 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements a mechanism to clear the internal EF Core service provider cache to address memory leaks in testing scenarios. The implementation adds a Clear() method to ServiceProviderCache and exposes it through a static method on EntityFrameworkMetrics and an extension method on DatabaseFacade.
Changes:
- Added
Clear()method toServiceProviderCacheto clear the internal configurations dictionary - Added static
ClearServiceProviderCache()entry point (intended but incomplete) - Added
DatabaseFacadeextension method to provide a public API surface - Added unit test to verify cache clearing functionality
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/EFCore/Internal/ServiceProviderCache.cs | Adds virtual Clear() method to clear the internal _configurations dictionary |
| src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs | Adds using statement for ServiceProviderCache namespace (but missing the actual static method implementation) |
| src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs | Adds DatabaseFacade extension method to call EntityFrameworkMetrics.ClearServiceProviderCache() |
| test/EFCore.Tests/ServiceProviderCacheTest.cs | Adds test that verifies cache clearing using reflection to check internal state |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
249ae47 to
6b86657
Compare
…om/Abde1rahman1/efcore into feature-clear-serviceProviderCache
|
@AndriySvyryd , Done. I've addressed all comments: updated the XML documentation with the concurrency warning, removed the redundant extension method as requested, and enhanced the test to verify functional behavior by asserting that a new service provider instance is created after clearing the cache |
Co-authored-by: Andriy Svyryd <AndriySvyryd@users.noreply.github.com>
Co-authored-by: Andriy Svyryd <AndriySvyryd@users.noreply.github.com>
|
Thanks for your contribution! |
Summary
This PR implements a "pubternal" way to clear the internal service provider cache, as requested in #27189. This allows developers to resolve memory leaks and state corruption issues, especially in heavy testing scenarios where many internal service providers are created.
Changes
Added
Clear()toServiceProviderCacheto allow clearing the internal_configurationsdictionary.Added a static
ClearServiceProviderCache()method toEntityFrameworkMetricsin theInfrastructure.Internalnamespace.Added a unit test in
ServiceProviderCacheTestto ensure the cache is effectively cleared.Tests for the changes have been added (for bug fixes / features)
Code follows the same patterns and style as existing code in this repo