-
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
Fix conflicting HttpClient configs #110999
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -416,7 +416,7 @@ public static IHttpClientBuilder AddHttpClient(this IServiceCollection services, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// be set to the type name of <typeparamref name="TClient"/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// be set to the type name of <typeparamref name="TImplementation"/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// <typeparam name="TClient"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// The type of the typed client. The type specified will be registered in the service collection as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -450,7 +450,7 @@ public static IHttpClientBuilder AddHttpClient(this IServiceCollection services, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AddHttpClient(services); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
string name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
string name = TypeNameHelper.GetTypeDisplayName(typeof(TImplementation), fullName: false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this introduces a breaking change. However, if you're looking to have multiple implementations for an interface, you can take advantage of the options available for named and typed HttpClients. It’s a great way to handle different scenarios! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are indeed several ways to achieve this, but this is something that ought to work as expected out of the box. With dependency injection, the expected behavior is to allow different implementations of an interface to coexist without conflicts. However, the current setup leads to issues, as evidenced by many people encountering this problem. Here’s a related discussion on StackOverflow that highlights this issue: https://stackoverflow.com/questions/74005464/add-httpclients-with-same-interface-ended-up-having-the-same-base-url-asp-net-co Also, what is the problem with changing this behavior? What do you think could go wrong with allowing multiple HttpClient registrations under the same interface to retain their independent configurations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that this code might be missing a few other implementations. Just wanted to point it out. Lines 707 to 716 in 6eba467
About your question:
I don't know, but @CarnaViire can help. I encourage you to explore these links and review the related issues. Lines 765 to 785 in 95814d0
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you consider this change acceptable, I would consider the modification of other overloads as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CarnaViire would you please clarify this? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var builder = new DefaultHttpClientBuilder(services, name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
builder.ConfigureHttpClient(configureClient); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net.Http; | ||
|
||
namespace Microsoft.Extensions.Http | ||
{ | ||
public class TestAnotherTypedClient : ITestTypedClient | ||
{ | ||
public TestAnotherTypedClient(HttpClient httpClient) | ||
{ | ||
HttpClient = httpClient; | ||
} | ||
|
||
public HttpClient HttpClient { get; } | ||
} | ||
} |
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.
This makes the documentation on the method (and any that call it) now incorrect. (line 440)
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.
You're right I'm gonna change the documentation