title: How to limit concurrent external API calls in .Net Core Web API?
body: <p>Currently i am working on a .net core web api project on which it is getting data from an external web api. And they have a concurrent rate limiter of 25(25 concurrent api calls are permitted) at their end. the 26th API call will be failed.</p>
  <p>So i want to implement a concurrent API rate limiter on my web API project and need to track 26th API call which is failed and need to retry that(it may be get or post call). i have multiple get request as well as post request in my api code</p>
  <p>following is my httpservice.cs in my web api</p>
  <pre><code>public HttpClient GetHttpClient()
  {
      HttpClient client = new HttpClient
      {
          BaseAddress = new Uri(APIServer),
      };
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
      client.DefaultRequestHeaders.Add(&quot;Authorization&quot;, (&quot;Bearer &quot; + Access_Token));
      return client;
  }
  private HttpClient Client;
  public async Task&lt;Object&gt; Get(string apiEndpoint)
  {
  
      Client = GetHttpClient();
      HttpResponseMessage httpResponseMessage = await Client.GetAsync(apiEndpoint);
      if (httpResponseMessage.IsSuccessStatusCode)
      {
          Object response = await httpResponseMessage.Content.ReadAsStringAsync();
          return response;
      }
      else if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
      {
          //need to track failed calls                    
          return StatusCode(httpResponseMessage.StatusCode.GetHashCode());
      }
  }
  
  public async Task&lt;Object&gt; Post(string apiEndpoint, Object request)
  {
      Client = GetHttpClient();
      HttpResponseMessage httpResponseMessage = await Client.PostAsJsonAsync(apiEndpoint, request);
      if (httpResponseMessage.IsSuccessStatusCode)
      {
          return await httpResponseMessage.Content.ReadAsAsync&lt;Object&gt;();
      }
  
      else if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
      {
          //need to track
          return StatusCode(httpResponseMessage.StatusCode.GetHashCode());
      }
  } 
  </code></pre>
  <p>how can i limit concurrent api calls in above example</p>
  <pre><code>SemaphoreSlim _semaphoregate = new SemaphoreSlim(25);
  await _semaphoregate.WaitAsync();      
  _semaphoregate.Release();  
  </code></pre>
  <p>Will this work ?</p>
  <p>AspNetCoreRateLimit nuget package is useful here ? will that limit  concurrency on above sample?</p>
  <p>Please help.</p>