Null values while updating DB in Parallel.Foreach

242 views Asked by At

I use following script to get data from external service and store in dB. In certain rare cases less than 1% records gets updated with null values. In below code, the "re.status=fail" we see null. let us know if any thots.

    public void ProcessEnquiries()
    {

        List<req> request = new List<req>();

        var options = new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxDegreeOfParallelism"]) };
        try
        {
            Parallel.ForEach(request, options, currentRequest =>
            {
                ProcessedRequest processedRequest = null;
                processedRequest = CommunicateToWS(currentRequest);  // Here we call to webservice
            });
        }
        catch (AggregateException exception)
        {
            foreach (Exception ex in exception.InnerExceptions)
            {
                // Handle Exception

            }            
       }
    }

    public ProcessedRequest CommunicateToWS(req objReq)
    {
         ProcessedRequest re = new ProcessedRequest();
        using (WebCall  obj = new WebCall())
        {

            re.no = refnu;
            try
            {
                retval = obj.getValue(inval);
                objProxy.Close();
                //get data
                // parse and store to DB
            }
            catch (Exception e)
            {
                 re.status = "fail";
                //update DB that request has failed
                //Handle Exception
                obj.Close();
            }
        }
    }
0

There are 0 answers