© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
115 replies
Thinker

✅ Best practice when using HttpClient in a class

I have a class which essentially acts as a wrapper over an HttpClient as an abstraction for a web API. This class has a field
private readonly HttpClient client;
private readonly HttpClient client;
which is currently set through the constructor of the class, where I'm just kind of expecting the consuming code to have configured the base address and any other configuration in regards to the client itself. However, I don't know if this is considered good practice, or if there's some other way I'm not aware of. This class should also be usable with DI, so I don't know if I instead should have an IHttpClientFactory in the constructor or something instead.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Best practice for class constructors?
C#CC# / help
2y ago
✅ Best practice usage of HttpClient in .Net Framework 4.5
C#CC# / help
4y ago
✅ Function argument class reference / clone (best practice(s))
C#CC# / help
9mo ago