Repository navigation
Named client #1636
Closed
Arthur-Lee
started this conversation in
General
Named client
#1636
Replies: 2 comments 1 reply
|
The standalone IRefitHttpClientFactory from PR #1418 was not kept, but the exact scenario you describe (two instances of the same interface, different base URLs, retrieved by name) is now supported directly through keyed DI: AddKeyedRefitClient(serviceKey, ...). builder.Services.AddKeyedRefitClient<ISomeApi>("one")
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://url_one"));
builder.Services.AddKeyedRefitClient<ISomeApi>("two")
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://url_two"));Resolve by key: public class Middleware(
[FromKeyedServices("one")] ISomeApi apiOne,
[FromKeyedServices("two")] ISomeApi apiTwo) { ... }
// or
var apiOne = serviceProvider.GetRequiredKeyedService<ISomeApi>("one");This is the Refit equivalent of resolving a named HttpClient by key, and it works when the interface/schema is shared across multiple downstream services. It requires the keyed-services support in .NET 8+ DI. Available in Refit.HttpClientFactory (see AddKeyedRefitClient overloads). |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi, I just found the PR #1418 which has already added named client. However I don't find the IRefitHttpClientFactory in the main branch.
Is there any plan to add this feature back? The named client can be very useful in some case.
All reactions