码迷,mamicode.com
首页 > 数据库 > 详细

asp.net Core HttpClient 出现Cannot access a disposed object. Object name: 'SocketsHttpHandler' 的问题。

时间:2019-04-17 12:14:50      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:var   request   com   pos   user   automatic   time   sock   primary   

 ASP.NET Core 部署在Centos 中 偶尔出现

One or more errors occurred. (Cannot access a disposed object.Object name: ‘SocketsHttpHandler‘.)

 之前的写法如下:

  var httpclientHandler = new HttpClientHandler();
            httpclientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
            httpclientHandler.AutomaticDecompression = DecompressionMethods.GZip;
            httpclientHandler.MaxRequestContentBufferSize = 102400;
            services.AddHttpClient("tmkhttp", option =>
            {
                option.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
                option.Timeout = new TimeSpan(0, 0, 10);
            }).ConfigurePrimaryHttpMessageHandler(() =>
            {
                return httpclientHandler;
            }); 

 

看提示应该是SocketsHttpHandler 资源释放了,没重新实例化。

在github上看到一个答案 说  ConfigurePrimaryHttpMessageHandler 里面不要直接return HttpClientHandler,要重新new 一下

改完后的如下:

 services.AddHttpClient("tmkhttp", option =>
            {
                option.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
                option.Timeout = new TimeSpan(0, 0, 10);
            }).ConfigurePrimaryHttpMessageHandler(() =>
            {
                return new HttpClientHandler()
                {
                    MaxRequestContentBufferSize = 102400 ,
                    AutomaticDecompression = DecompressionMethods.GZip ,
                    ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true
                };
            
            }); 

 

asp.net Core HttpClient 出现Cannot access a disposed object. Object name: 'SocketsHttpHandler' 的问题。

标签:var   request   com   pos   user   automatic   time   sock   primary   

原文地址:https://www.cnblogs.com/wangjun8868/p/10722633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!