How to Implement a Generic HttpClient Wrapper in .NET 8
Why we need generic HttpClient Wrapper?
In .NET, making HTTP calls to 3rd-party APIs and services is an easy task with the help of HttpClient. However, when it comes to production, careful consideration is necessary before incorporating HttpClient directly into the codebase. Injecting HttpClient everywhere and making external API calls without proper management can be problematic. Therefore, it is advisable to use a generic wrapper to handle external API calls.
The Generic HttpClient Wrapper serves as a solution in such scenarios. This wrapper encapsulates all HttpClient logic, providing a clean and organized approach. By using this wrapper in our services, we can enhance the maintainability and readability of the codebase.
Let's dive into the code of HttpClient Wrapper and explore its usage:
Interface:
Class:
Let’s understand this code step by step:
Constructor: The class has a constructor that takes an instance of the HttpClient class. This is important for dependency injection, allowing the class to use an existing HttpClient instance.
GetAsync Method: This method performs an HTTP GET request. It takes a generic type for the expected response (TResponse) and an optional request object (TRequest). It constructs the URL with an optional query string based on the provided request. If the HTTP response indicates an error, it throws an exception. Otherwise, it reads the response body and deserializes it into the specified response type using Newtonsoft.Json.
PostAsync Method: Similar to GetAsync, this method performs an HTTP POST request. It takes a generic type for the expected response (TResponse), a request object (TRequest), and a content type. It serializes the request object into JSON and sends it as the request content. If the HTTP response indicates an error, it throws an exception. Otherwise, it reads the response body and deserializes it into the specified response type using Newtonsoft.Json.
Register your HttpClient Wrapper into your DI container.
Where to put your HttpClient Wrapper in your Clean Architecture?
HttpClient is external concern so put the Wrapper class in your infrastructure project & your Interface of HttpClient Wrapper will go into your application or domain layer.
Benefits:
Reduced Code Duplication: Encapsulate common HTTP request logic in one place.
Improved Maintainability: Easily modify and extend functionalities in a centralised class.
Type Safety and Clarity: Generic methods ensure correct return types and promote consistent usage.
Reusable Building Block: Leverage the wrapper across different parts of your application.
Additional Considerations:
Logging: Implement logging for request/response details and improved debugging.
Retries and Timeouts: Consider retrying failed requests and setting timeouts for better control.
Authentication: Integrate authentication strategies for secure API access.
Further Customisation: Expand functionalities like header manipulation, custom serialisation, and response validation.
Conclusion:
In summary, the Generic HttpClient Wrapper in .NET offers a systematic approach to manage HTTP calls, ensuring code cleanliness and maintainability, especially when interacting with external APIs. By encapsulating HttpClient logic, it minimises code duplication, promotes consistency, and facilitates easier modifications. Placing the wrapper in the infrastructure layer aligns with Clean Architecture principles. The wrapper's benefits, including reduced redundancy and enhanced re-usability, make it a valuable tool for developers, though attention to aspects like logging and customisation is crucial for optimal functionality. Overall, it serves as a key component for efficient and organised external API integration in applications.





