title: Async Loop There is no longer an HttpContext available
body: <p>I have a requirement, is to process X number of files, usually we can receive around 100 files each day, is a zip file so I have to open it, create a stream then send it to a WebApi service which is a workflow, this workflow calls two more WebApi Steps.</p>
  
  <p>I implemented a console application that loops through the files then calls a wrapper which makes a REST call using HttpWebRequest.GetResponse().</p>
  
  <p>I stressed tested the solution and created 11K files, in a synchronous version it takes to process all the files around 17 minutes, but I would like to create an async version of it and be able to use await HttpWebRequest.GetResponseAsync().</p>
  
  <p>Here is the Async version:</p>
  
  <pre><code>private async Task&lt;KeyValuePair&lt;HttpStatusCode, string&gt;&gt; REST_CallAsync(
          string httpMethod,
          string url,
          string contentType,
          object bodyMessage = null,
          Dictionary&lt;string, object&gt; headerParameters = null,
          object[] queryStringParamaters = null,
          string requestData = "")
      {
          try
          {
              HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("some url");
              req.Method = "POST";
              req.ContentType = contentType;
  
                  //Adding zip stream to body
                  var reqBodyBytes = ReadFully((Stream)bodyMessage);
                  req.ContentLength = reqBodyBytes.Length;
                  Stream reqStream = req.GetRequestStream();
                  reqStream.Write(reqBodyBytes, 0, reqBodyBytes.Length);
                  reqStream.Close();                
              //Async call
              var resp = await req.GetResponseAsync();
              var httpResponse = (HttpWebResponse)resp as HttpWebResponse;
              var responseData = new StreamReader(resp.GetResponseStream()).ReadToEnd();                
              return new KeyValuePair&lt;HttpStatusCode,string&gt;(httpResponse.StatusCode, responseData);
          }
          catch (WebException webEx)
          {
          //something
          }
          catch (Exception ex)
          {
          //something
          }
  </code></pre>
  
  <p>In my console Application I have a loop to open and call the async (CallServiceAsync under the covers calls the method above)</p>
  
  <pre><code>foreach (var zipFile in Directory.EnumerateFiles(directory))
                                  {
                                          using (var zipStream = System.IO.File.OpenRead(zipFile))
                                          {
                                              await _restFulService.CallServiceAsync&lt;WorkflowResponse&gt;(
                                              zipStream,
                                              headerParameters,
                                              null,
                                              true);
                                          }
                                          processId++;
                                      }
                                  }
  </code></pre>
  
  <p>What end up happening was that only 2K of 11K got processed and didn't throw any exception so I was clueless so I changed the version I am calling the async to:</p>
  
  <pre><code>foreach (var zipFile in Directory.EnumerateFiles(directory))
                                  {      
                                          using (var zipStream = System.IO.File.OpenRead(zipFile))
                                          {
                                              tasks.Add(_restFulService.CallServiceAsync&lt;WorkflowResponse&gt;(
                                              zipStream,
                                              headerParameters,
                                              null,
                                              true));
                                          }
                                      }
                                  }
  </code></pre>
  
  <p>And have another loop to await for the tasks:</p>
  
  <pre><code>foreach (var task in await System.Threading.Tasks.Task.WhenAll(tasks))
                  {
                      if (task.Value != null)
                      {
                          Console.WriteLine("Ending Process");
                      }
                  }
  </code></pre>
  
  <p>And now I am facing a different error, when I process three files, the third one receives:</p>
  
  <p>The client is disconnected because the underlying request has been completed. There is no longer an HttpContext available.</p>
  
  <p>My question is, what i am doing wrong here? I use SimpleInjector as IoC would it be this the problem?</p>
  
  <p>Also when you do WhenAll is waiting for each thread to run? Is not making it synchronous so it waits for a thread to finish in order to execute the next one? I am new to this async world so any help would be really much appreciated.</p>