title: Concurrent HttpClient calls throwing 500 internal server error
body: <p>I'm using <a href="https://github.com/paulcbetts/refit" rel="nofollow">Refit</a> with 1-2 interfaces and generate my client RESTful classes for Web API requests.</p>
  
  <pre><code>public interface IApiClient&lt;T&gt; where T : new()
      {
          [Get("")]
          Task&lt;IEnumerable&lt;T&gt;&gt; GetInitialOrUpdatedData(string clientId);
      }
  </code></pre>
  
  <p>In a class I have 60 properties like below.</p>
  
  <pre><code>public IApiClient&lt;Order&gt; Orders { get; private set; }
  public IApiClient&lt;OrderDetail&gt; OrderDetails { get; private set; }
  public IApiClient&lt;Company&gt; Companies { get; private set; }
  public IApiClient&lt;User&gt; Users { get; private set; }
  public IApiClient&lt;Address&gt; Addresses { get; private set; }
  public IApiClient&lt;Product&gt; Products { get; private set; }
  // ...
  </code></pre>
  
  <p>And intialized like below.</p>
  
  <pre><code>Orders  = RestService.For&lt;IApiClient&lt;Order&gt;&gt;(GetHttpClient("/Order"));
  OrderDetails = RestService.For&lt;IApiClient&lt;OrderDetail&gt;&gt;(GetHttpClient("/OrderDetail"));
  Companies = RestService.For&lt;IApiClient&lt;Company&gt;&gt;(GetHttpClient("/Company"));
  Users = RestService.For&lt;IApiClient&lt;User&gt;&gt;(GetHttpClient("/User"));
  // ...
  </code></pre>
  
  <p>The GetHttpClient(string) returns a new HttpClient instance for each resource endpoint.</p>
  
  <pre><code>private const string BaseUrlAddress = @"http://yourURL/api";
  
  private HttpClient GetHttpClient(string address)
  {
      HttpClient httpClient = new HttpClient(new NativeMessageHandler())
      {
          BaseAddress = new Uri(BaseUrlAddress + address),
          Timeout = TimeSpan.FromMinutes(3)
      };
      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      return httpClient;
  }
  </code></pre>
  
  <p>Somewhere in a galaxy far away I have repositories that will make concurrent calls to all these APIs in the following manner, note that I'm using <a href="https://github.com/paulcbetts/punchclock" rel="nofollow">Punchclock</a> to limit the calls to 5 at a time.</p>
  
  <pre><code>private OperationQueue _queue = new OperationQueue(5 /* at a time */);
  private const int LowPriority = 1;
  private const int NormalPriority = 5;
  private const int HighPriority = 10;
  </code></pre>
  
  <p>The list of tasks in a method call.</p>
  
  <pre><code>IEnumerable&lt;Task&gt; tasks = new List&lt;Task&gt;
              {
                  _queue.Enqueue(NormalPriority, () =&gt; Orders .GetAllAsync(clientId)),
                  _queue.Enqueue(NormalPriority, () =&gt; OrderDetails .GetAllAsync(clientId)),
                  _queue.Enqueue(NormalPriority, () =&gt; Companies .GetAllAsync(clientId)),
                  _queue.Enqueue(NormalPriority, () =&gt; Users .GetAllAsync(clientId)),
                  _queue.Enqueue(NormalPriority, () =&gt; Addresses .GetAllAsync(clientId))
          };
  
          await Task.WhenAll(tasks).ConfigureAwait(false);
  </code></pre>
  
  <p>The above will throw 500 internal server errors to random tables as I noticed. Changing the calls to serially asynchronous works great!</p>
  
  <pre><code>await Orders.GetAllAsync(workerId).ConfigureAwait(false);
  await OrderDetails .GetAllAsync(workerId).ConfigureAwait(false);
  await Companies .GetAllAsync(workerId).ConfigureAwait(false);
  await Users .GetAllAsync(workerId).ConfigureAwait(false);
  await Addresses .GetAllAsync(workerId).ConfigureAwait(false);
  </code></pre>
  
  <p>It is kinda impossible to understand the 500 internal server when it is not consistent and since my server works handling the calls one by one I'm trying to understand if this is a client side problem of the APIs I'm using or my server needs special configuration to handle the requests, which 5 at a time is descent to handle.</p>
  
  <p>I'm using Web API 2 server.</p>
  
  <p>I'm also thinking if I can reuse the same HttpClient instance with different endpoint configuration with my setup.</p>
  
  <p>Any thoughts much appreciated.</p>