-
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
Adding multiple HttpClients with same Interface results in conflicting configurations #110996
Comments
It looks like IHttpClientBuilder.Name is "ITypedClient" for both TypedClientA and TypedClientB, and that is then used as ConfigureNamedOptions<HttpClientFactoryOptions>.Name, so both callbacks are added to the same HttpClientFactoryOptions.HttpClientActions list. A workaround might be: builder.Services.AddHttpClient<ITypedClient, TypedClientB>(httpClient =>
{
if (httpClient is TypedClientB)
{
httpClient.BaseAddress = new Uri("https://exampleB.com");
}
}); but this wouldn't help if you wanted two differently-configured instances of the same implementation type TypedClientB. |
This is not a fix |
Right, it was for the "Known Workarounds" section. |
Changed DefaultHttpClientBuilder name and added new test Fix dotnet#110996
Maybe you should use this |
Thank you for your suggestion, but I want to clarify that I’m not looking for a workaround for this issue. My goal is to address and fix it for good. While there are alternative ways to handle this scenario, the current behavior is counterintuitive and inconsistent with the expected functionality of dependency injection. This is something that should work out of the box without requiring extra workarounds. |
Description
When registering multiple
HttpClient
services usingHttpClientFactoryServiceCollectionExtensions.AddHttpClient
with the same interface, the resulting services share the same configuration (e.g.,BaseAddress
). This behavior leads to unexpected results when injecting these services as anIEnumerable
into a controller or service.Reproduction Steps
Program.cs
:BaseAddress
:Expected behavior
When multiple
HttpClient
instances are registered using the same interface withAddHttpClient
, each implementation should retain its unique configuration. Specifically:TypedClientA
should be configured with aBaseAddress
ofhttps://exampleA.com
.TypedClientB
should be configured with aBaseAddress
ofhttps://exampleB.com
.When these services are injected as
IEnumerable<ITypedClient>
into a controller or service, the collection should contain both implementations, each retaining its respective configuration. For example:TypedClientA
should make HTTP requests tohttps://exampleA.com
.TypedClientB
should make HTTP requests tohttps://exampleB.com
.The system should correctly distinguish between the two implementations without overwriting or merging their configurations.
Actual behavior
When multiple
HttpClient
instances are registered using the same interface withAddHttpClient
, the configurations for the services are overwritten. Specifically:TypedClientA
andTypedClientB
end up sharing the sameBaseAddress
, which is determined by the last registered configuration.For example:
TypedClientB
is registered afterTypedClientA
, both clients will have theBaseAddress
set tohttps://exampleB.com
.BaseAddress
set tohttps://exampleA.com
.When these services are injected as
IEnumerable<ITypedClient>
into a controller or service, both implementations refer to the sameHttpClient
configuration, resulting in unexpected and incorrect behavior.Regression?
This issue has been present since at least .NET 7 and continues to occur in .NET 8 and .NET 9. The behavior where multiple
HttpClient
instances with the same interface overwrite each other's configurations has persisted across these versions.Testing with previous versions of .NET (7, 8, and 9) confirms that this issue has been ongoing, suggesting it may be a long-standing bug or unintended behavior in the framework's
AddHttpClient
method.Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: