【问题标题】:ServicePointManager in ASP.NET CoreASP.NET Core 中的 ServicePointManager
【发布时间】:2016-09-18 16:52:37
【问题描述】:

我正在尝试将现有的类库代码转换为 .NET Core 类库。在 static 构造函数的代码中,我有以下内容:

ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;

我做了一些搜索,ServicePointManager 在 .NET Core 中不再可用,现在应该使用WinHttpHandler (ServicePointManager.DefaultConnectionLimit in .net core?)。

我的问题是 ServicePointManager 到底是什么,正在设置的属性是什么?

WinHttpHandler 不是 staticServicePointManager 一样,所以我必须创建一个实例来设置这些属性?我是否需要更改所有 http 调用才能使用该实例?

【问题讨论】:

  • 你能移植 pushsharp 吗?我正在做这件事,并假设这就是你正在做的事情。

标签: c# asp.net-core .net-core asp.net-core-1.0


【解决方案1】:

WinHttpHandler 继承自 HttpMessageHandler,因此您可以在构造 HttpClient 时将其作为参数传递,如下所示:

WinHttpHandler httpHandler = new WinHttpHandler();
httpHandler.SslProtocols = SslProtocols.Tls12;

HttpClient client = new HttpClient(httpHandler);

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    ServicePointManager 在 .NET Core v2.0 中可用

    组装 System.Net.ServicePoint,版本=4.0.0.0,文化=中性,PublicKeyToken=cc7b13ffcd2ddd51

    也就是说,我已经使用以下方法实现了我的 HTTP 客户端调用:

    System.Net.Http.HttpClientHandler handler;  // use DI etc to create 
    if (options.SkipSsl)  // config driven
        handler.ServerCertificateCustomValidationCallback = (req, cer, ch, err) => true;
    

    https://stackoverflow.com/a/44540071/4292717

    【讨论】:

      【解决方案3】:

      只需添加以下命名空间:

      using System.Net.Http;
      
      using System.Net.Http.Headers;
      

      然后改变你的行如下,

      WinHttpHandler httpHandler = new 
      WinHttpHandler();
      
      httpHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
      

      而不是

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | 
      SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
      

      编码愉快!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 2015-01-11
        • 1970-01-01
        • 2015-07-11
        • 2016-10-21
        相关资源
        最近更新 更多