How can I send a lot of webservice requests really quickly using C#?

Viewed 32

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($"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!

1 Answers

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://docs.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://docs.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.

Related