我正在向具有不同数据的特定 Web 服务发送大量并发请求。为了实现这一点,我创建了许多线程(大约 50 个)。每分钟的总请求数可能会增加到 10000。Windows 服务形式的应用程序可以正常运行几分钟,然后遇到操作超时错误。
我已经尝试了常见的怀疑方法,例如增加DefaultConnectionLimit、关闭 Web 响应对象。由于请求不会在服务器上花费太多时间,因此我还将请求超时和ReadWriteTimeout 设置为 5 秒。下面是被不同线程重复调用的代码片段。
// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;
// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte[] bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST"; …Run Code Online (Sandbox Code Playgroud)