extensions icon indicating copy to clipboard operation
extensions copied to clipboard

Two httpclients configured by httpclientfactory for classes with same name conflict

Open blalonde opened this issue 7 years ago • 5 comments

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.

blalonde avatar Jan 17 '19 13:01 blalonde

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

rynowak avatar Feb 19 '19 19:02 rynowak

Fixed by https://github.com/aspnet/Extensions/pull/1563

rynowak avatar May 01 '19 18:05 rynowak

Hello @rynowak ! This fix is not available in .net core 2?

netbidev avatar Jul 23 '19 19:07 netbidev

This is a change in behavior - in this case introducing stricter validation. We don't port changes like this to previous releases.

rynowak avatar Jul 23 '19 20:07 rynowak

I see, thanks.

netbidev avatar Jul 23 '19 20:07 netbidev