I am looking for a best practice solution for the next task.
I use ASP.NET Core 6.0.
I created an SDK which works with public API. The SDK communicates with API via Http request. I use HttpClient
class to make http requests.
My goal is to build a Nuget package so consumers will be able to just install this SDK and communicate with the public API.
In the end we have two projects :
- Consumer project
- SDK project
Question: where do I have to instantiate an instance of HttpClient
class?
My thoughts: if I do it in the Sdk project, and just use:
HttpClient client = new HttpClient();
This is not good. Because it is bad practice to create an instance of HttpClient class via new()
. See
https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines
According this guide from MS, I have to initiate this instance via DI in the consumer project:
services.AddHttpClient();
If I select this options I thinks It is will be hard for client to register my SDK, client must do two steps:
- Register
HttpClient
in DI container - Register SDK in container
Is it possible to reduce the amount of steps and make using my library easier?