liveBook · Manning

SocketsHttpHandler

As explained in ASP.NET Core in Action, 3e

SocketsHttpHandler is the .NET HTTP message handler that implements HTTP networking with managed sockets and supports connection pooling and recycling.

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.
CH33_F04_Lock3

Trace how HttpClient uses a handler pipeline whose final network handler connects to the remote server and sends the request. section 33.2.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains replacement, performance, platform consistency, and pooling, but defers the manual pooling configuration details.
Examples
Uses an ASP.NET Core endpoint that creates an HttpClient through IHttpClientFactory and calls a rates API.
What it assumes
Assumes familiarity with HttpClient and dependency injection while showing the factory registration and creation steps.
What you can do after
Operate/configure HTTP clients with factory-managed handlers or manually enabled pooling in non-DI applications.
How it connects
Connects HttpClient, IHttpClientFactory, HttpClientHandler, connection pooling, and dependency injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.