Recently read about how httpclient should be reused and not disposed. In the original implementation, we instantiate httpclient every time it is used which opens many tcp connections unnecessarily. Different parts of the app using httpclient call different endpoints and some even use certificates.
Having trouble on how to architect its usage in an MVC application. Using a static httpclient per controller in a static constructor seemed like a good idea, but some usage is with a webrequesthandler that adds a client certificate. How do I go about handling replacement certificates without having to restart the app pool?
private static readonly HttpClient _httpClient;
static MyController()
{
var clientHandler = new WebRequestHandler();
var certPath = HostingEnvironment.MapPath("~/Certificates");
string[] files = Directory.GetFiles(certPath, "*.pfx");
foreach (var file in files)
{
clientHandler.ClientCertificates.Add(GetClientCertificate(file));
}
_httpClient = new HttpClient(clientHandler);
}