SocketsHttpHandler implements .NET HTTP networking with managed sockets. From .NET 5 onward, it replaces the legacy HttpClientHandler and provides performance and cross-platform consistency benefits. section 33.2.1
Use it with the appropriate lifetime-management approach:
- Configure connection pooling and recycling manually when working without dependency injection or IHttpClientFactory. section 33.2.1
- Prefer IHttpClientFactory in dependency-injection applications; it manages pooled handlers and adds named and typed clients. section 33.2.1
Factory-managed HTTP client
Register IHttpClientFactory, inject it into an endpoint, create an HttpClient, set its BaseAddress, and call the remote API. The factory manages the handler lifetime while application code remains focused on the request. section 33.2.1
Figure 33.4 Each HttpClient contains a pipeline of HttpMessageHandlers. The final handler is an HttpClientHandler, which makes the connection to the remote server and sends the HTTP request. This configuration is similar to the ASP.NET Core middleware pipeline, and it allows you to make cross-cutting adjustments to outgoing requests.
Trace how HttpClient uses a handler pipeline whose final network handler connects to the remote server and sends the request. section 33.2.1
