0

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();
            }
        }
    }
Sarav
  • 117
  • 1
  • 13
  • Where is the null occurring? Is the exception you're catching in `CommunicateToWS` a `NullReferenceException`? – Ann L. Dec 04 '13 at 12:14
  • 1
    What is the exception? Where exactly it fails? Is it possible that it fails because objProxy is being shared between threads? Where objProxy comes from? Please try to debug your code first :) – Alexey Raga Dec 04 '13 at 12:15
  • Updated the proxy object.. it was a typo as i can't publish actual code.Ann, am setting ProcessedRequest object's status property to "fail" which is not getting set – Sarav Dec 04 '13 at 12:20
  • Do you return `re` to the caller? Given that `re` goes out of scope when `obj` does, I don't see how you can even tell that it isn't getting set. – Ann L. Dec 04 '13 at 12:27
  • I am manipulating the code to give the prob scenario. re is declared outside.. – Sarav Dec 04 '13 at 13:18

0 Answers0