-
Notifications
You must be signed in to change notification settings - Fork 5.2k
HttpClientFactory Keyed DI APIs #104943
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
Merged
Merged
HttpClientFactory Keyed DI APIs #104943
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0328667
HttpClientFactory Keyed DI APIs (WIP)
CarnaViire 5062c9e
add tests
CarnaViire 44997b7
Optimize opt-out tracking
CarnaViire 84bedd2
Merge branch 'main' into hcf-keyed-api
CarnaViire dbcba0b
Merge remote-tracking branch 'upstream/main' into hcf-keyed-api
CarnaViire f82eeee
Rename APIs, add tests, fix Handler lifetime
CarnaViire e958285
Add workaround for empty name HttpClient fallback
CarnaViire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
92 changes: 92 additions & 0 deletions
92
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/HttpClientKeyedLifetime.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Net.Http; | ||
| using Microsoft.Extensions.Http; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| internal class HttpClientKeyedLifetime | ||
| { | ||
| public static readonly HttpClientKeyedLifetime Disabled = new(null!, null!, null!); | ||
|
|
||
| public object ServiceKey { get; } | ||
| public ServiceDescriptor Client { get; } | ||
| public ServiceDescriptor Handler { get; } | ||
|
|
||
| public bool IsDisabled => ReferenceEquals(this, Disabled); | ||
|
|
||
| private HttpClientKeyedLifetime(object serviceKey, ServiceDescriptor client, ServiceDescriptor handler) | ||
CarnaViire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ServiceKey = serviceKey; | ||
| Client = client; | ||
| Handler = handler; | ||
| } | ||
|
|
||
| private HttpClientKeyedLifetime(object serviceKey, ServiceLifetime lifetime) | ||
| { | ||
| ThrowHelper.ThrowIfNull(serviceKey); | ||
| ServiceKey = serviceKey; | ||
| Client = ServiceDescriptor.DescribeKeyed(typeof(HttpClient), ServiceKey, CreateKeyedClient, lifetime); | ||
| Handler = ServiceDescriptor.DescribeKeyed(typeof(HttpMessageHandler), ServiceKey, CreateKeyedHandler, lifetime); | ||
| } | ||
|
|
||
| public HttpClientKeyedLifetime(ServiceLifetime lifetime) : this(KeyedService.AnyKey, lifetime) { } | ||
| public HttpClientKeyedLifetime(string name, ServiceLifetime lifetime) : this((object)name, lifetime) { } | ||
|
|
||
| public void AddRegistration(IServiceCollection services) | ||
| { | ||
| if (IsDisabled) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| services.Add(Client); | ||
| services.Add(Handler); | ||
CarnaViire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void RemoveRegistration(IServiceCollection services) | ||
| { | ||
| if (IsDisabled) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| services.Remove(Client); | ||
| services.Remove(Handler); | ||
| } | ||
|
|
||
| private static HttpClient CreateKeyedClient(IServiceProvider serviceProvider, object? key) | ||
| { | ||
| if (key is not string name || IsKeyedLifetimeDisabled(serviceProvider, name)) | ||
| { | ||
| return null!; | ||
| } | ||
| return serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(name); | ||
rokonec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static HttpMessageHandler CreateKeyedHandler(IServiceProvider serviceProvider, object? key) | ||
| { | ||
| if (key is not string name || IsKeyedLifetimeDisabled(serviceProvider, name)) | ||
| { | ||
| return null!; | ||
| } | ||
| HttpMessageHandler handler = serviceProvider.GetRequiredService<IHttpMessageHandlerFactory>().CreateHandler(name); | ||
| // factory will return a cached instance, wrap it to be able to respect DI lifetimes | ||
| return new LifetimeTrackingHttpMessageHandler(handler); | ||
| } | ||
|
|
||
| private static bool IsKeyedLifetimeDisabled(IServiceProvider serviceProvider, string name) | ||
| { | ||
| HttpClientMappingRegistry registry = serviceProvider.GetRequiredService<HttpClientMappingRegistry>(); | ||
rokonec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!registry.KeyedLifetimeMap.TryGetValue(name, out HttpClientKeyedLifetime? registration)) | ||
| { | ||
| registration = registry.DefaultKeyedLifetime; | ||
| } | ||
|
|
||
| return registration?.IsDisabled ?? false; | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.