C
C#4mo ago
Eple

Question regarding multiple calls to AddHttpClient()

I have a class library that calls this:
services.AddHttpClient();
services.AddHttpClient();
Then the main project calls this:
builder.Services.AddHttpClient();
builder.Services.AddHttpClient();
Can it cause any issues?
7 Replies
peter
peter4mo ago
Hi, how are you? plz type in chatGPT.
Angius
Angius4mo ago
Kindly, fuck off with this sort of an answer Unlikely to cause issues, but just strictly unnecessary
peter
peter4mo ago
I am sorry.
Eple
Eple4mo ago
Thanks for your response. Then I am wondering how the double call to AddHttpClient() can be avoided – as both projects need the IHttpClientFactory injected, but none knows if the other has added it.
Angius
Angius4mo ago
One thing that comes to mind is using typed clients That way, the library would have it's own client, and the app would have it's own (or multiple) Could maybe make it internal too, so the app cannot just inject it
PixxelKick
PixxelKick4mo ago
Your business layer shouldn't be aware of the service collection at all really. That's an application layer concern. Business layer really should just be defining all your services and not worrying about how the services get their dependencies. Application layer actually handles sewing it all together with DI and running it
Ryan Chen
Ryan Chen4mo ago
It's no problem, the AddHttpClient method will only add if it hasn't been added before. You can see https://source.dot.net/#Microsoft.Extensions.Http/DependencyInjection/HttpClientFactoryServiceCollectionExtensions.cs,72bc67c4aadb77fc
Want results from more Discord servers?
Add your server