Two httpclients configured by httpclientfactory for classes with same name conflict
Describe the bug
Configuring httpclients with httpclientfactory for two classes with the same name but in different namespace causes both classes to have the same httpclient instead of different ones.
To Reproduce
Steps to reproduce the behavior:
[Fact]
public void Test()
{
var services = new ServiceCollection();
services.AddSingleton<IClassA, ClassA>();
services.AddHttpClient<IClassA, ClassA>().ConfigureHttpClient(c => c.Timeout = TimeSpan.FromSeconds(1));
services.AddSingleton<A2.IClassA, A2.ClassA>();
services.AddHttpClient<A2.IClassA, A2.ClassA>().ConfigureHttpClient(c => c.Timeout = TimeSpan.FromSeconds(2));
var provider = services.BuildServiceProvider();
var classA1 = provider.GetService<IClassA>();
var classA2 = provider.GetService<A2.IClassA>();
classA1.HttpClient.Timeout.Should().Be(TimeSpan.FromSeconds(1));
classA2.HttpClient.Timeout.Should().Be(TimeSpan.FromSeconds(2));
}
namespace A1
{
public class ClassA : IClassA
{
public HttpClient HttpClient { get; }
public ClassA(HttpClient httpClient)
{
this.HttpClient = httpClient;
}
}
}
namespace A1
{
public interface IClassA
{
HttpClient HttpClient { get; }
}
}
namespace A2
{
public class ClassA : IClassA
{
public HttpClient HttpClient { get; }
public ClassA(HttpClient httpClient)
{
this.HttpClient = httpClient;
}
}
}
namespace A2
{
public interface IClassA
{
HttpClient HttpClient { get; }
}
}
Expected behavior
Test should pass : Both classes should get their own httpclient
Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
Bug probably caused here : DefaultHttpClientBuilder builder = new DefaultHttpClientBuilder(services, typeof (TClient).Name);
should use FullName instead i guess.
Thanks - this is intended for now. I'm planning to add some guard rails for this so that you know to configure an explicit name. See also: https://github.com/aspnet/Extensions/issues/519
Fixed by https://github.com/aspnet/Extensions/pull/1563
Hello @rynowak ! This fix is not available in .net core 2?
This is a change in behavior - in this case introducing stricter validation. We don't port changes like this to previous releases.
I see, thanks.