Asynchronous calls from web api become weired

Topics: ASP.NET MVC, ASP.NET Web API
Nov 8, 2013 at 4:28 PM
I have the following code in my system at present.
      public static XmlDocument Submit(XmlDocument serviceRequest)
    {
        HttpWebRequest hwRequest = (HttpWebRequest)System.Net.WebRequest.Create(clsWebConfig.getAppSetting("rm_ServiceURL"));
        byte[] postData = Encoding.UTF8.GetBytes("xmlreqdoc=" + serviceRequest.OuterXml);
        hwRequest.Method = "POST";
        .....Request Preparatetion code

        HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse();

        Stream hwResponseStream = hwResponse.GetResponseStream();
       .....Code to Read Data to XMLDOc
        return xdResponse;
    }
I am trying to make this asynchronous and did the following
   private  XmlDocument xdResponse;
public XmlDocument Submit(XmlDocument serviceRequest)
    {
       HttpWebRequest hwRequest = (HttpWebRequest)System.Net.WebRequest.Create(clsWebConfig.getAppSetting("rm_ServiceURL"));

        byte[] postData = Encoding.UTF8.GetBytes("xmlreqdoc=" + serviceRequest.OuterXml);
        hwRequest.Method = "POST";
        .....Request Preparatetion code

      Task<WebResponse> responseTask = Task.Factory.FromAsync<WebResponse>(hwRequest.BeginGetResponse, hwRequest.EndGetResponse, null);
        XmlDocument xdResponse = new XmlDocument();
        using (var responseStream = responseTask.Result.GetResponseStream())
        {
            //Read data from stream to XML Doc
        }
        return xdResponse;
    }
Getting an exception now. result is coming as null.

The actual code which call this functions are given below. I am invoking a list of Service calls in parellel. Intention is to reduce the response time and that is why i am trying to make the service calls also async. Taks inside the parallel foreach is also taking delays. Some times it is taking a second or half a second to make the next call, which is infact a big delay. If i have 10 calls to make, it is taking more than 6 secs to complete the activity. That is not the intention

public XmlDocument GetRMOffersForMTN(DDOfferRequest offer)
    {
        XmlDocument xDocument = new XmlDocument();
        Debug.Print("Start" +DateTime.Now.ToString("hh.mm.ss.ffffff"));
        var tasks = new List<Task>();
        foreach (string mmd in offer.MTN)
        {
            string data = mmd;
            var task = new Task(() => RunServiceCall(Inputs);
            tasks.Add(task);
        }
        Parallel.ForEach(tasks, task => task.Start());
        try
        {
            Task.WaitAll(tasks.Where(x => x != null).ToArray());
           return XmlDocument after parsing data

        }
        catch (AggregateException ae)
        {
            throw ae.Flatten();
        }
    }

    public void RunServiceCall(DDOfferRequest request)
    {
        WebRequest webRequest = new WebRequest();
        Debug.Print("Service Call " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
        XmlDocument xRequest = BuildServiceRequest(request);
        XmlDocument xResponse = webRequest.Submit(xRequest);
    }
My Submit method must send the XmlDocument back when finished.

Here is my Trase output from server telling the time the calls are made on server

Start11.49.04.675741
Service Call 11.49.04.676741
Service Call 11.49.04.676741
Service Call 11.49.04.676741
Service Call 11.49.04.676741
Service Call 11.49.04.677741
Service Call 11.49.05.426816
Service Call 11.49.05.998873
Service Call 11.49.06.137887
Service Call 11.49.06.184892
Service Call 11.49.06.310905
Service Call 11.49.06.438917
Task Ends11.49.07.773051

Seems the parellel foreach is not working as expected.

Looking for some experts comments