title: ASP.NET web API - System.Threading.Tasks.TaskCanceledException: A task was canceled dilemma
body: <p>I have an asp.net web API. (net framework 4.6.1) I am calling a 3rd party rest API (Product) in one of my actions. In functional tests, everything works as expected, no problem. But when I do spike tests (let's say, 100 users, 10 seconds ramp-up time) I am getting <code>System.Threading.Tasks.TaskCanceledException: A task was canceled error</code> after some successful responses. I googled and came across that it might be due to timeout. I added <code>TimeSpan.FromSeconds(600);</code> but still getting the same error.</p>
  
  <p>Here is how I call this 3rd party API. Is there any problem with an async call? Would you please have a look at my code? </p>
  
  <pre><code>public class UtilitiesTest
      {
          private static readonly HttpClient _httpClient = new HttpClient();
  
  
         //some not relevant code here
  
          public static async Task&lt;HttpResponseMessage&gt; CallRazer(GameRequest gameRequest, string url)
          {
              try
              {
                  FormUrlEncodedContent content = null;
  
                  if (url == "Product/")
                  {
                      try
                      {
                          //some code here
  
  
                          //Timeout
                          _httpClient.Timeout = TimeSpan.FromSeconds(600);
                          //Call Game
                          var response = await _httpClient.PostAsync("https://test.com/" + url, content);
                          return response;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e);
                          throw;
                      }
                      finally
                      {
                          content.Dispose();
                      }
                  }
                  else
                  {
                      try
                      {
                          //some code here
  
                          //Call Game
                          var response = await _httpClient.PostAsync("https://test.com/" + url, content);
                          return response;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e);
                          throw;
                      }
                      finally
                      {
                          content.Dispose();
                      }
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
                  throw;
              }
          }
  
  
      }
  </code></pre>
  
  <p>and here is how I call Razer:</p>
  
  <pre><code>private async Task&lt;HttpResponseMessage&gt; CallProducts()
          {
              //some code here
  
              #region Call Razer for products
  
  
              //**Call Razer**
              var response = await Utilities.CallRazer(products, "Product/");
  
              var htmlResponse = await response.Content.ReadAsStringAsync();
              var model = JsonConvert.DeserializeObject&lt;ProductResponseDto&gt;(htmlResponse);
  
             // some code here
  
              return response;
          }
  </code></pre>
  
  <p>Somebody in another forum, suggested me to use HTTPWeb Request instead of Httpclient. So I changed my code like below and all my troubles are gone.</p>
  
  <pre><code>//HTTPWebRequest
  var request = (HttpWebRequest) WebRequest.Create("http://test.com" + url);
  request.ContentType = "application/x-www-form-urlencoded";
  request.Method = "POST";
  
  var keyValueContent = productRequest.ToKeyValue();
  var formUrlEncodedContent = new FormUrlEncodedContent(keyValueContent);
  var urlEncodedString = await formUrlEncodedContent.ReadAsStringAsync();
  
  using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
  {
       streamWriter.Write(urlEncodedString);
  }
  
  HttpWebResponse httpResponse = (HttpWebResponse) (await request.GetResponseAsync());
  
  response = new HttpResponseMessage
  {
       StatusCode = httpResponse.StatusCode,
       Content = new StreamContent(httpResponse.GetResponseStream()),
  };
  
  return response;
  
  </code></pre>