title: C# Semaphores not working as expected, cannot figure out why
body: <p>I am building an ASP.NET (.NET Framework) application in C# I am making API calls to a backend service called "LinuxDocker" and I am trying to limit the number of 12 concurrent calls to it from the ASP.NET application.  Here is the code I wrote:</p>
  
  <pre><code>private static Semaphore LinuxDockerSemaphore = new Semaphore(12, 12);
  
  public static SemaphoreWaiter WaitForLinuxDocker(int timeoutMS = -1)
  {
        return new SemaphoreWaiter(LinuxDockerSemaphore, timeoutMS);
  }
  
  public class SemaphoreWaiter : IDisposable
  {
      Semaphore Slim;
  
      public SemaphoreWaiter(Semaphore slim, int timeoutMS = -1)
      {
          Slim = slim;
          Slim.WaitOne(timeoutMS);
      }
  
      public void Dispose()
      {
          Slim.Release();
      }
  }
  </code></pre>
  
  <p>Then when I call my backend service, I do so like this:</p>
  
  <pre><code>using (ConcurrencyManager.WaitForLinuxDocker())
  {
        // Call backend service here
  }
  </code></pre>
  
  <p>So it seems like this should limit to 12 concurrent calls, however when I test it from an integration test with 100 simultaneous calls, it basically only allows 1 request at a time to go through, not 12 calls at a time to go through.</p>
  
  <p>The service is running on IIS on Windows Server 2016 and .NET Framework 4.7.</p>
  
  <p>I have read this code many times and can't figure out why it doesn't work.</p>
  
  <p>Any help would be greatly appreciated.</p>