BT

Fixing .NET’s HttpClient

| by Jonathan Allen Follow 612 Followers on Mar 01, 2018. Estimated reading time: 2 minutes |

As we reported back in 2016, .NET's HttpClient has some serious issues. With the introduction of HttpClientFactory in .NET Core 2.1, some of those issues have been mitigated.

The fundamental problem with HttpClient is that it looks like a per-call resource that should be disposed immediately after use. In actuality, it is meant to be created once per destination server and kept for the life of the application. Except when it's not, such as when needing to clear the DNS cache.

The new HttpClientFactory allows you to pre-configure HttpClients on application startup. Each configuration is named, making it easier to request a HttpClient with the correct configuration. An unnamed configuration is also provided for cases where custom configuration isn't needed.

Because strings can be error prone, the configuration for an HttpClient can also be keyed to a specific type. For example, you can create a configuration for the GitHubService class. Then the Dependency Injection (DI) framework will ensure that instances of GitHubService will be provided with a HttpClient using that configuration rather than the default.

This new framework for managing HttpClients does not change the basic rules for their use: it is still dangerous to dispose an HttpClient. In regards to the DNS caching issue, Karel Zikmund writes,

We are actively discussing the design to address DNS changes problem directly in ManagedHandler. Currently we're considering 2 options: either DNS TTL support (#24257) or ConnectionLeaseTimeout-like API (#26331). We will make sure that the DNS changes scenario is addressed in combination with HttpClientFactory feature in 2.1 timeframe.

Message Handlers and Automatic Retries

A little-known feature of HttpClient is the ability to add message handlers. Out of the box, the HttpClientHandler controls features such as whether or not to allow auto-redirects, which cookie container to use, and how pre-authentication works.

This can be further extended by implementing a subclass of DelegatingHandler. In a demonstration on Channel 9, Daniel Roth shows how to create a simple retry handler.

The HttpClientFactory compliments this capability by allowing developers to include HttpClientHandlers as part of the HttpClient configuration. This is intentionally done in a way to be DI friendly.

SocketHttpHandler

The HttpClientHandler subsystem isn't just for modifying behavior; it can also completely alter the way a HttpClient communicates with the network. Microsoft is taking advantage of this by introduction the SocketHttpHandler. This is a "new from-the-ground-up managed HttpClientHandler" that eliminates the "platform dependencies on libcurl (for linux) and WinHTTP (for Windows)". The announcement continues,

You can opt-in to using the SocketHTTPHandler in one the following ways with Preview 1:

Environment variable: COMPlus_UseManagedHttpClientHandler=true

AppContext: System.Net.Http.UseManagedHttpClientHandler=true

You can also enable sockets mode for ASP.NET. Eventually this may become the default with the current native implementations being opt-in.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Poor HttpClient! by Nima Niazmand

Why it took so long?

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT