title: Asp.net Web API hangs while reading from Http Client post method (SOAP Request/Response)
body: <p>Web API method is getting into a deadlock for larger response. Can someone point out where it is getting deadlock. The same code is invoked in a for loop.
  Here is the code</p>
  <pre><code>  public async Task&lt;string&gt; GetXMLResponse(string baseUrl, string fullFileName, int countryId, HttpClient client, FileOperations objFileOperation, string outputfilePath)
      {
  
          try
          {
              var fileData = new StringBuilder(objFileOperation.ReadFile(fullFileName));
              var content = new StringContent(TransformToSOAPXml(fileData, countryId), Encoding.UTF8,
                  &quot;application/xml&quot;);
              //fileData.Clear();
              //fileData.Capacity = 0;
              SerilogMethods.LogCustomException(log, &quot;Before sending to Tax Engine&quot;, &quot;GetXMLResponse&quot;);
              using (HttpResponseMessage response = await client.PostAsync(baseUrl, content))
              {
  
                  // content = null;
                  response.EnsureSuccessStatusCode();
                  if (response.IsSuccessStatusCode)
                  {
                      SerilogMethods.LogCustomException(log, &quot;Started Recieving response from Tax Engine&quot;, &quot;GetXMLResponse&quot;);
                      var resultContent = await response.Content.ReadAsStreamAsync();
                      StreamReader readStream = new StreamReader(resultContent, Encoding.UTF8);
                      var postresult = await readStream.ReadToEndAsync();
                      XDocument doc = XDocument.Parse(postresult);
                      doc.Save(outputfilePath);
                      readStream.Dispose();
                      resultContent.Dispose();
  
                      //File.WriteAllText(outputfilePath, await response.Content.ReadAsStringAsync());
  
                      SerilogMethods.LogCustomException(log, &quot;File write Completed&quot;, &quot;GetXMLResponse&quot;);
                     
                      return &quot;Success&quot;;
                  }
  
                  SerilogMethods.LogCustomException(log, &quot;Error occured in Tax Engine&quot;, &quot;GetXMLResponse&quot;);
                  //Log the SOAP response when the SOAP fails with an error message
                  if (response.Content != null)
                  {
                      throw new Exception(await response.Content.ReadAsStringAsync());
                  }
                  return null;
              }
  
          }
          catch (Exception ex)
          {
              SerilogMethods.LogError(log, ex, &quot;GetXMLResponse&quot;);
              return null;
  
          }
  
  
      }
  </code></pre>
  <p>If I uncomment and comment the other lines, it works with File.WriteAllText</p>
  <pre><code>                    File.WriteAllText(outputfilePath, await response.Content.ReadAsStringAsync());
  </code></pre>
  <p>Can someone point out why the second solution works and not the first one.I have to even parse the xml response before saving it</p>