我正在向具有不同数据的特定Web服务发送大量同时请求.为了实现这一点,我创建了多个线程(数量大约为50).每分钟的请求总数可能会增加到10000. Windows服务形式的应用程序可以在几分钟内正常运行,然后遇到操作超时错误.
I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000. The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.
我尝试了常见的可疑措施,例如增加 DefaultConnectionLimit ,关闭网络响应对象.由于请求在服务器上不需要花费很多时间,因此我还将请求 Timeout 和 ReadWriteTimeout 设置为5秒. 下面是由不同线程重复调用的代码段.
I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds. Below is the code snippet which is called repeatedly by different threads.
// 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"; request.Timeout = 5000; request.ReadWriteTimeout = 5000; request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password)); request.CookieContainer = this.cookieContainer; if (body != null) { Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); } HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { responseText = streamReader.ReadToEnd(); } httpResponse.Close();推荐答案
ServicePointManager.DefaultConnectionLimit限制了发往给定服务器的Web请求的数量.默认设置通常为 2或10 .
ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.
如果要对该Web服务进行50次并行调用,则应将(应用程序启动时)ServicePointManager.DefaultConnectionLimit设置为更大的数字(例如 40-50 ).
If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).
此外,您没有在request上调用Close或Dispose.您应该这样做,或者让 using 照顾好它为你.
Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.
更多推荐
向主机发送大量请求时,该操作在System.Net.HttpWebRequest.GetResponse()处超时
发布评论