HttpClient and Delegating Handlers in .Net Core
The purpose of the article here is to quickly show some code for:
Registering a HttpClient with Net Core DI
Configure the HttpClient
Add a DelegatingHandler to a HttpClient
Use Net Code DI to provide a HttpClient to a consumer
HttpClient issues in .NetFramework
System.Net.Http.HttpClient is a class that can be used to send Http Requests and receive Http Responses.
In .NetFramework applications, there are a couple of pitfalls around using HttpClient.
Disposing of HttpClients does not (always) result in its connections being cleaned up properly. Since network connections are resources of the underlying operating system, they are not managed by the .NET Framework. Heavy use of HttpClient can result in connection starvation on the applications host machine.
This is detailed in the article YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE
To mitigate this issue, HttpClient is often created as a singleton and shared throughout an application. This leads to the second issue, where HttpClient does not respect DNS changes and consequently fails when an endpoint moves to another IP address (such as in a failover scenario)
.Net Core HttpClientFactory to the rescue
.Net Core provides a HttpClientFactory which manages and pools HttpClientMessageHandlers to avoid the DNS and connection starvation issues.
Its also a central place for naming and configuring HttpClient instances
HttpClientFactory also enables you to configure the pipeline of message handlers that HttpClient uses, which can enable you to do all sorts of fancy things, for example:
Log Requests
Adding Headers
Blocking Certain Requests
Mocking for Unit Testing
Generally, you will use the HttpClientFactory middleware to configure your HttpClients at startup, where you are also wiring up your dependencies. When you are ready to use a HttpClient, you get an instance of IHttpClientFactory and call CreateClient on it to return you an instance of HttpClient.
In the following code snippets, I show one way you might configure a HttpClient, and include a logging message handler to log each request / response.
(It should be noted that System.Net.Http has a built in LoggingMessageHandler that can be used in HttpClient, but its just to illustrate how to add an arbritraty MessageHandler
In the following gist, we create a ServiceCollection, add some simple logging, register our HttpMessageHandlers, register a HttpClient and finally retrieve the HttpClient from IServiceProvider and use it make a HttpRequest.
RegisterHttpMessageHandlers and RegisterHttpClient are extension methods that I have defined elsewhere (and they are included below in the next gist)
https://gist.github.com/systemundertestcode/11476be44a4144c4253957223c84d089
RegisterHttpMessageHandlers registers a LoggingHandler on the ServiceCollection and takes a lambda for resolving an ILogger.
RegisterHttpClient registers a HttpClient using the AddHttpClient extension from System.Net.Http, and adds a DelegateHandler to HttpClient via the resolveLoggingHttpMessageHandler delegate
https://gist.github.com/systemundertestcode/10ad8173779e5f5dcc6d60c149ca9b03
The next gist has the LoggingHandler. It inherits from DelegatingHandler and so it can be added to the request / response pipeline of an HttpClient.
You can see this LoggingHandler being added in RegisterHttpClient
.AddHttpMessageHandler(resolveLoggingHttpMessageHandler);
https://gist.github.com/systemundertestcode/ed4fda1fdc8c16c5eb74c80cfcd5ecd9
To sum up:
Net Core provides a HttpClientFactory, that can be used to configure and create HttpClients that can be injected via DI into your services. The factory pools and manages HttpClients underlying Message Handlers, so that it is not susceptible to the issues that it became known for in .NetFramework.













