reactiveui / refit

The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.
https://reactiveui.github.io/refit/
MIT License
8.67k stars 745 forks source link

[Bug]: HttpClient named client with the same name overwrite ConfigureHttpClient BaseAddress #1686

Open squadwuschel opened 7 months ago

squadwuschel commented 7 months ago

Describe the bug 🐞

I am using named clients to Authorize my client access, and here I have multiple APIs using the same Named client. Now I am creating multiple Refit Clients for multiple Interfaces (multiple different API URLs) and all use the same Named client to authenticate.

When I Inject the API Client Interfaces into my services/controller, from all Interfaces the BaseAdress is the last baseAddress from my Registered Refit Clients.

Step to reproduce

Here you can see my setup:

  services.AddHttpClient("GitHubClient", client =>
        {
            client.BaseAddress = new Uri("https://api.github.com/");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            // Weitere Konfigurationen hier ...
        });

  services
       .AddRefitClient(typeof(IArticlesApi), null, $"GitHubClient")
       .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://myArticlesApi.com"));

  services
       .AddRefitClient(typeof(IContentApi), null, $"GitHubClient")
       .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://myContentApi.com"));

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

when I now inject the "IContentApi" and the "IArticlesApi" into an Controller or Service/Provider the Client.BaseAdress on both injected Interfaces is "https://myContentApi.com" => the last registered c.BaseAddress. That seems to be wrong I would say, because, when I Use multiple different named clients, then each BaseAddress is like expected.

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows 10

Version

No response

Device

No response

Refit Version

7.0.0

Additional information ℹ️

No response

derekm commented 3 weeks ago

This is expected behavior. You're just reconfiguring the same IHttpClientFactory, and the actual clients aren't instantiated until injection time, and the factory produces different instances for each injection site, they are not all the same reference to the same client.