gpt4 book ai didi

.Net Core HttpClientFactory 配置 HttpClientHandler

转载 作者:行者123 更新时间:2023-12-03 15:49:27 31 4
gpt4 key购买 nike

我目前正在配置我的 httpclientfactory这边走

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

};

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

要禁用安全证书的验证,但在执行太多请求时,我收到此异常:

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



我目前正在构建我的 httpclient这边走
HttpClient client = _httpClientFactory.CreateClient("ignoreSSL");

做测试,这个是这样解决的,不用 httpclientfactory
HttpClient ad = new HttpClient(handler, false);

多我找我找不到怎么告诉 httpclientfactory不让处理程序处理?

最佳答案

该问题是由于单个 HttpClientHandler 实例用于所有请求而引起的。管理、汇集和回收处理程序是 HttpClientFactory 的工作。

看起来实际的问题是如何在使用 HttpClientFactory 时禁用证书验证。这需要配置它使用的客户端处理程序。

As the documentation shows这是使用 ConfigurePrimaryHttpMessageHandler 完成的方法。从方法的注释:

Remarks

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



由该方法创建的处理程序将被添加到处理程序池中,并由其他处理程序(如 Polly 的重试处理程序)使用。 HttpClientFactory 将回收旧的处理程序来处理 DNS 注册更改。

代码应如下所示:
services.AddHttpClient<HttpWrapper>("ignoreSSL")
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (m, crt, chn, e) => true
};
});

关于.Net Core HttpClientFactory 配置 HttpClientHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256792/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com