I am trying to iterate over a list of 20,000+ customer records. I am using a Parallel.ForEach() loop to attempt to speed up the processing. Inside the delegate function, I am making an HTTP POST to an external web service to verify the customer information. In doing so, the loop is limited to 2 threads, or logical cores. If I attempt to increase the Degree of Parallelism, the process throws an error "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server"
Is this default behavior of the loop when working with external processes or a limitation of the receiving web server?
My code is rather straight forward:
Parallel.ForEach ( customerlist, new ParallelOptions {MaxDegreeOfParallelism = 3 },( currentCustomer ) =>
{
if ( IsNotACustomer ( currentCustomer.TIN ) == true ) <--IsNotCustomer is where the HTTP POST takes place
{
...Write data to flat file...
}
});
If I change the MaxDegreesOfParallelism to 2 the loop runs fine.
This code takes about 80 minutes to churn through 20,000 records. While that is not unacceptable, if I could shorten that time by increasing the number of threads, then all the better.
Full exception message (without stack trace):
System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
at System.Net.HttpWebRequest.GetResponse()
Any assistance would be greatly appreciated.
EDIT
The HTTP POST code is:
HttpWebRequest request = ( HttpWebRequest )WebRequest.Create ( AppConfig.ESLBridgeURL + action );
request.Method = "POST";
request.GetRequestStream ( ).Write ( Encoding.UTF8.GetBytes ( body ), 0, body.Length );
Stream stream = request.GetResponse ( ).GetResponseStream ( );
StreamReader reader = new StreamReader ( stream );
output = reader.ReadToEnd ( );
The URL is to an in-house server running proprietary Web Sphere MQ services. The gist of which is to check internal data sources to see whether or not we have a relationship with the customer.
We run this same process in our customer relationship management process in hundreds of sites per day. So I do not believe there is any licensing issue and I am certain these MQ services can accept multiple calls per client.
EDIT 2
A little more research has shown the 2 connection limit is valid. However, using a ServicePointManager may be able to bypass this limitation. What I cannot find is a C# example of using the ServicePointManager with HttpWebRequests.
Can anyone point me to a valid resource or provide a code example?