Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core HttpClientFactory dispose HttpClientHandler

I am currently configuring my httpclientfactory this way

HttpClientHandler httpClientHandler = new HttpClientHandler()
{
    ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }

};

serviceCollection.AddHttpClient("ignoreSSL", c =>
{}).ConfigurePrimaryHttpMessageHandler(h => httpClientHandler);

To disable the validation of security certificates, but when doing too many requests I receive this exception:

Cannot access a disposed object. Object name: 'SocketsHttpHandler'.

I currently build my httpclient this way

HttpClient client = _httpClientFactory.CreateClient("ignoreSSL");

Doing tests, this is solved in this way without httpclientfactory

HttpClient ad = new HttpClient(handler, false);

For more than I look for I can not find how to tell httpclientfactory not to make handler dispose?

like image 979
Davis Cruz Avatar asked Jan 18 '19 15:01

Davis Cruz


People also ask

Should I dispose HttpClientHandler?

There is no need to dispose of the HttpClient instances from HttpClientFactory. Disposal will not actually do anything in this case because the factory manages the handler and connection lifetimes and not the HttpClient instances.

What is HttpClientHandler C#?

The HttpClient class uses a message handler to process the requests on the client side. The default handler provided by the dot net framework is HttpClientHandler. This HTTP Client Message Handler sends the request over the network and also gets the response from the server.

What is HttpClientFactory?

NET Core 2.1 introduced two approaches, one of them being IHttpClientFactory. It's an interface that's used to configure and create HttpClient instances in an app through Dependency Injection (DI). It also provides extensions for Polly-based middleware to take advantage of delegating handlers in HttpClient.

What is AddHttpClient?

AddHttpClient(IServiceCollection) Adds the IHttpClientFactory and related services to the IServiceCollection. AddHttpClient(IServiceCollection, String) Adds the IHttpClientFactory and related services to the IServiceCollection and configures a named HttpClient.


1 Answers

The problem is caused because a single HttpClientHandler instance is used for all requests. It's the HttpClientFactory's job to manage, pool and recycle handlers.

It looks like the actual question is how to disable certificate validation when using HttpClientFactory. This requires configuring the client handlers used by it.

As the documentation shows this is done using the ConfigurePrimaryHttpMessageHandler method. From the method's remarks :

Remarks

The delegate should return a new instance of the message handler each time it is invoked.

The handlers created by that method will be added to a handler pool and used by other handlers like Polly's retry handlers. HttpClientFactory will recycle older handlers to handle DNS registration changes.

The code should look like this:

services.AddHttpClient<HttpWrapper>("ignoreSSL")
        .ConfigurePrimaryHttpMessageHandler(() =>
        {
            return new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (m, crt, chn, e) => true
            };
        });
like image 191
Panagiotis Kanavos Avatar answered Oct 22 '22 02:10

Panagiotis Kanavos