fix: HttpClientWithCache could not be resolved from DI#6
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6 +/- ##
==========================================
+ Coverage 69.24% 71.04% +1.79%
==========================================
Files 26 26
Lines 1099 1112 +13
Branches 132 133 +1
==========================================
+ Hits 761 790 +29
+ Misses 319 301 -18
- Partials 19 21 +2
🚀 New features to boost your workflow:
|
|
After testing the PR in the actual branch, I can confirm that the core issue is resolved – both What Works
Areas for Discussion (not blockers)1. API Signature Change- HttpCacheOptions? cacheOptions = null
+ Action<HttpCacheOptions>? configureCacheOptions = nullQuestion: Is this breaking change intentional? The original issue didn't request API changes. Analysis:
Options:
2.
|
|
While the Concerns:
Suggestion: Consider a simpler approach that maintains backward compatibility: // Keep original signature (no breaking changes)
public static IServiceCollection AddHttpClientWithCache(
this IServiceCollection services,
string? httpClientName = null,
HttpCacheOptions? cacheOptions = null)
// Add overload for Action<T> pattern when needed
public static IServiceCollection AddHttpClientWithCache(
this IServiceCollection services,
string? httpClientName,
Action<HttpCacheOptions> configureCacheOptions)
{
var options = new HttpCacheOptions();
configureCacheOptions(options);
return AddHttpClientWithCache(services, httpClientName, options);
}This would:
|
- implement overloads to support both Action<T> cache options configuration and direct instance injection approach for backward compatibility; - add missing unit test of proper concrete and abstractions DI resolution
|
Thanks for your in-depth investigation and feedback! I have followed suggested approach but also had to add backward compatible overloads for |
|
I've checked the latest commit. The solution is:
Ready for merge 🚀 |
Description
This pull request addresses the issue where the
HttpClientWithCacheinstance could not be directly resolved as its concrete type through dependency injection (DI). The changes modify theHttpClientWithCacheExtensionsclass to support configuration viaAction<HttpCacheOptions>instead of directly passingHttpCacheOptions, leveragingIOptionsSnapshot<HttpCacheOptions>for named client configuration retrieval. A new private factory method,CreateHttpClientWithCache, was added to handle the creation ofHttpClientWithCacheinstances, utilizingIOptionsSnapshotto fetch cache options, ensuring proper DI resolution. New unit tests inHttpClientWithCacheExtensionsTestswere written to verify the correct registration and configuration of the HTTP client with caching, including named client support and cache options application.These changes improve the flexibility and usability of the HTTP client with caching by aligning with standard .NET DI practices and ensuring proper configuration scoping.
Type of Change
Testing
Checklist
Additional Notes
CreateHttpClientWithCachemethod includes details about parameters and possible exceptions.