-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpClientFactory Keyed DI APIs #104943
HttpClientFactory Keyed DI APIs #104943
Conversation
Note regarding the
|
Note regarding the
|
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, not an expert here though 😄
@halter73 @JamesNK @rokonec @benjaminpetit PTAL, if possible 🙏 |
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientBuilderExtensions.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientBuilderExtensions.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still prefer it if we could make keyed DI work by default. We should fix #102654 regardless and backport the fix to .NET 8 imo. This seems good to me though.
It's too bad we couldn't resolve #47091 at least for people that use keyed services, but I understand it's a hard problem. And it would probably be better to have a one-size-fits-all solution for typed clients and keyed clients.
This won't affect the performance of resolving non-keyed HttpClientFactory services, right? Either way, I expect the cost of DI to be extremely small compared to the costs of making HTTP requests.
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
Show resolved
Hide resolved
I wonder if the simplest/best thing to do for now would be to throw a |
I've thought about it, but then decided that nothing really prevents the users to make the keyed registrations manually themselves, and still end up with the same problem (I've already seen folks doing that, as we didn't have the integration ready in 8.0). Also, the same problem is there for any transient IDisposable registrations now -- and we allow the users to register them still. I was considering getting in touch with the Analyzer team and see maybe we could have some analyzer warnings as a middle ground. And at least we have a scoped lifetime selected by default. I'd also expect that the keyed clients would mostly get injected in constructors, rather than instantiated directly via ServiceProvider. (arguably, injecting HttpClientFactory and calling CreateClient looks better than injecting ServiceProvider and calling GetRequiredKeyedService). Singletons have a limited number of instances, and as for scoped services, the transient dependencies would be all cleaned up when the scope finishes. |
Fixes #89755
UPD: Updated to the approved API shape
Included:
ConfigureHttpClientDefaults
, both for opt-in and opt-out, in any orderNot included
(yet?):(do not allow transients?)cc @halter73 @JamesNK @davidfowl @stephentoub @ManickaP