简体   繁体   English

如何管理最大连接数,HttpClient,C#?

[英]How to manage max connection, HttpClient, C#?

I'm working on a windows service. 我正在Windows服务上。 Code of windows service is very simple but it has a strange manner! Windows服务的代码非常简单,但是方式却很奇怪!
In windows service I should call a WebApi each 20 seconds and save the result in a SQL Database 在Windows服务中,我应该每20秒调用一次WebApi并将结果保存到SQL数据库中
I'm using autofac to creat instance of HttpClient at Program.cs 我正在使用autofacProgram.cs上创建HttpClient实例

// HttpClient
builder.Register(ctx =>
{
    var httpClient = new HttpClient
    {
        BaseAddress = new Uri(StaticAssets.WebApiBaseAddress)
    };
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return httpClient;
});

I never dispose httpClient instance manually. 我从不手动处理httpClient实例。
After running the windows service about 8 hours, IIS not work and we can not remote to server anymore, we tested Windows Remote Desktop Connection, VNC(we have VNC on server) ... 在运行Windows服务大约8个小时后,IIS无法正常工作,我们无法再远程访问服务器,我们测试了Windows远程桌面连接,VNC(服务器上有VNC)...
We could remote to server with KVM and stop the windows service and every thing go back. 我们可以使用KVM远程访问服务器并停止Windows服务,然后一切都会恢复。 I could find from here the problem is because of connection numbers! 我从这里可以发现问题是由于连接号! I'm not sure yet? 我不肯定?
Also I found the following: 我也发现以下内容:

<system.web>
 <processModel maxWorkerThreads="100" maxIoThreads="100" minWorkerThreads="50"/>
 <httpRuntime minFreeThreads="704" minLocalRequestFreeThreads="608"/>
</system.web>

<system.net>
 <connectionManagement>
  <add address="[ProvideIPHere]" maxconnection="96"/>
 </connectionManagement>
</system.net>

From https://support.microsoft.com/en-us/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s https://support.microsoft.com/zh-cn/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s

What is the best practice to manage max connections in HttpClient .Net windows service & IIS? HttpClient .Net Windows服务和IIS中管理最大连接的最佳实践是什么?

The solution base on googling: 基于谷歌搜索的解决方案:

1. Define HttpClient as SingleTone and don't dispose it manually. 1.将HttpClient定义为SingleTone,不要手动进行处理。
In AutoFac use something like the following: AutoFac使用如下所示的内容:

builder.Register(ctx =>
{
    var httpClient = new HttpClient
    {
        BaseAddress = new Uri('https://www.sample.com/')
    };
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return httpClient;
}).SingleInstance();

In .Net Core use the following: .Net Core使用以下命令:

services.AddSingleton(provider =>
{
    var httpClient = new HttpClient
    {
        BaseAddress = new Uri('https://www.sample.com/')
    };
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return httpClient;
});

2. Add the following in your config file 2.在config文件中添加以下内容

<configuration>
    ....
    <system.web>
     <processModel maxWorkerThreads="100" maxIoThreads="100" minWorkerThreads="50"/>
     <httpRuntime minFreeThreads="704" minLocalRequestFreeThreads="608"/>
    </system.web>

    <system.net>
     <connectionManagement>
      <add address="[ProvideIPHere]" maxconnection="96"/>
     </connectionManagement>
    </system.net>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM