-->

Parallel Foreach webservice call

2019-08-30 10:38发布

问题:

We have implemented application wherein we need to process incoming batch. for example a set of Request of certain object type has to be sent to particular webservice to have it processed

We have implemented following snippet to do so. need your help / guidance if there wouldbe any pitfalls on same

var options = new ParallelOptions { MaxDegreeOfParallelism = 10 };

Parallel.ForEach(request, options, currentRequest =>
{
    ProcessedRequest processedRequest = null;
    try
    {
        currentRequest.DBSave = true;

        processedRequest = CommunicateToService(currentRequest);

    }
    catch (Exception ex)
    {

        ExceptionManager.HandleException(ex);
    }
});

Inside CommunicateToservice method we will be calling the service and pass the request and get the response object and save to MS SQL DB around 10 -15 tables. The whole method is wrapped with AggregateException.

Need inputs on How the MaxDegreeOfParallelism value can be decided.

回答1:

For IO-bound work there is no easy guideline. You don't know what the point of optimal throughput is. Test different values and measure which one is the fastest.

You probably should not set the DOP too high because that might overload the remote service.