There has been talk recently of how we should be handling the lifetime of the System.Net.Http.HttpClient.This article http://byterot.blogspot.co.uk/2016/07/singleton-httpclient-dns.html says that we should use it as a singleton, but we need to be careful about ensuring the connections are properly closed otherwise DNS changes won't be honoured.I came up with the following message handler which can be injected in to an HttpClient to take care of setting the connection lease for each distinct scheme + host combination.public class ServicePointManagingMessageHandler : HttpClientHandler{    private readonly ConcurrentDictionary<Uri, ServicePoint> _uriCache = new ConcurrentDictionary<Uri, ServicePoint>();    private readonly int _oneMinuteInSeconds = (int)TimeSpan.FromMinutes(1).TotalSeconds;    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)    {        if (request.RequestUri.IsAbsoluteUri)        {            var baseUri = new Uri(${request.RequestUri.Scheme}://{request.RequestUri.Host});            _uriCache.AddOrUpdate(baseUri, uri =>            {                var sp = ServicePointManager.FindServicePoint(uri);                sp.ConnectionLeaseTimeout = _oneMinuteInSeconds;                return sp;            }, (uri, point) => point);        }        return base.SendAsync(request, cancellationToken);    }}Usage:var client = new HttpClient(new ServicePointManagingMessageHandler());I am wondering if the concept of intercepting the URI's as they come in to the HttpClient will allow the HttpClient to safely be used as a singleton without any of the issues listed in that article.This gets around needing to know all hostnames that you might connect to at startup by intercepting calls - if people agree with this approach then it seems like it should become the accepted way to use the HttpClient for standard cases.
Ensuring HttpClient can be used safely as a singleton
c#