如何使用C#真正快速地发送大量Web服务请求?
我正在研究一个程序,该程序将从我们的数据库中请求信息,将该信息发送到Web服务,然后将其登录到WebService响应回到数据库。
这是一种调用Web服务的方法,它使用其他一些自定义类,但一切都应该是相当自我解释的。
public void doTransitTimeLoop(string botName)
{
while (keepProcessingTransitTime)
// loop until keep processing is set to false from the UI thread
{
// send the start query
DataTable dt = sendSqlQuery($"execute fex.transitStart @Bot='{botName}', @TransitCount={fexTransitInstaces}");
List<TransitTimePackage> packages = new List<TransitTimePackage>();
// TransitTimePackage is pretty much just a container class
string returnQuery = "";
// loop through rows on datatable
foreach (DataRow row in dt.Rows)
// create a package object from each row
{
TransitTimePackage package = TransitTimePackage.createFromDataRow(this, row);
packages.Add(package);
ThreadPool.QueueUserWorkItem((object stateInfo) => {
RestResponse res = package.sendWebserviceRequest();
package.parseResponse(res);
returnQuery += package.sqlQuery + "\n";
});
}
// check if all the packages are done, if they aren't, sleep 1 sec
while (packages.Any(p => p.StatusCode == HttpStatusCode.Created))
Thread.Sleep(1000);
DataTable d = sendSqlQuery(returnQuery);
}
}
目前,我只是将所有网络服务调用推入ThreadPool,以便它们可以同时运行,但这只是让我每分钟大约100个呼叫,因为ThreadPool一次只能处理4个线程。理想情况下,我可以尽快发送我的所有电话,然后等待所有电话完成并继续执行我的代码,但是我不确定是否可能(网络不是我的strongsuit aha)。
任何帮助更快的帮助,都将不胜感激。谢谢!
I'm working on a program that will request information from our database, send that information to a webservice, then log to webservice response back to the database.
Here is the method that will call the webservice, it uses a few other custom classes but everything should be fairly self-explanatory.
public void doTransitTimeLoop(string botName)
{
while (keepProcessingTransitTime)
// loop until keep processing is set to false from the UI thread
{
// send the start query
DataTable dt = sendSqlQuery(quot;execute fex.transitStart @Bot='{botName}', @TransitCount={fexTransitInstaces}");
List<TransitTimePackage> packages = new List<TransitTimePackage>();
// TransitTimePackage is pretty much just a container class
string returnQuery = "";
// loop through rows on datatable
foreach (DataRow row in dt.Rows)
// create a package object from each row
{
TransitTimePackage package = TransitTimePackage.createFromDataRow(this, row);
packages.Add(package);
ThreadPool.QueueUserWorkItem((object stateInfo) => {
RestResponse res = package.sendWebserviceRequest();
package.parseResponse(res);
returnQuery += package.sqlQuery + "\n";
});
}
// check if all the packages are done, if they aren't, sleep 1 sec
while (packages.Any(p => p.StatusCode == HttpStatusCode.Created))
Thread.Sleep(1000);
DataTable d = sendSqlQuery(returnQuery);
}
}
I'm currently just pushing all the webservice calls into the threadpool so they can run concurrently, but that's only letting me make about 100 calls a minute because ThreadPool can only handle 4 threads at a time. Ideally I could send all my calls as quickly as possible, then wait for them all to finish and keep executing my code, but I'm not sure if that's possible (networking isn't my strongsuit aha).
Any help to get this running faster would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么您的ThreadPool仅产生4个线程;我什至不知道这是否确实如此。但是,如果是这样,您可以尝试使用
threadpool.setminthreads
( https://learn.microsoft.com/en-en-us/dotnet/api/api/api/system.threadpool.threadpool.threadpool.setpool.setpool.setminthreads?views? )您的另一个选项是使用asynchronous套接字(请参阅 https://learn.microsoft.com/en-us/dotnet/api/api/system.sockets.sockets.sockets.socket.beginconnect?view=net-6.0 )并打开大量异步插座,而无需每个插座一个线。
I do not know why your threadpool only spawns 4 threads; I do not even know whether this is in fact so. But if it is, you can try increasing the number of threads using
ThreadPool.SetMinThreads
(https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=net-6.0)Your other option would be to use asynchronous sockets (See https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.beginconnect?view=net-6.0) and open a large number of asynchronous sockets without having to have one thread per socket.