public HttpClient GetOrCreateWithCertificate(string clientCertificateThumbprint, string certificateAuthorityThumbprint) { // Check if the client certificates exists before each use of the http client // If the client does not exist GetPrivateCertificate method will throw an exception. var clientCertifcate = _x509CertificateManager.GetPrivateCertificate(clientCertificateThumbprint); return(_clients.GetOrCreate(clientCertificateThumbprint ?? string.Empty + certificateAuthorityThumbprint, e => { e.Size = 1; e.SlidingExpiration = _httpClientExpiration; e.RegisterPostEvictionCallback((k, v, r, s) => { // This delegate will be called when the http client has not been used for 10 minutes. if (v is IDisposable disposable) { try { disposable.Dispose(); } catch { } } }); return _httpClientFactory.CreateWithCertificate(clientCertifcate, certificateAuthorityThumbprint); })); }