title: C# Implementing Producer/Consumer Queue In a Web Service
body: <p>I have a fairly vanilla web service (old school asmx).  One of the methods kicks off some async processing that has no bearing on the result returned to the client.  Hopefully, the little snippet below makes sense:</p>
  
  <pre><code>[System.Web.Services.WebMethod]
  public List&lt;Foo&gt; SampleWebMethod(string id)
  {
      // sample db query
      var foo = db.Query&lt;Foo&gt;("WHERE id=@0",id);
  
      // kick of async stuff here - for example firing off emails
      // dont wait to send result
      DoAsyncStuffHere();
  
      return foo;
  
  }
  </code></pre>
  
  <p>My initial implementation for the DoAsyncStuffHere method made use of the ThreadPool.QueueUserWorkItem.  So, it looks something like:</p>
  
  <pre><code>public void DoAsyncStuffHere()
  {
      ThreadPool.QueueUserWorkItem(delegate
      {
          // DO WORK HERE
      });
  }
  </code></pre>
  
  <p>This approach works fine under low load conditions.  However, I need something that can handle a fairly high load.  So, the producer/consumer pattern would seem to be the best way to go.  </p>
  
  <p>Where I am confused is how to constrain all work being done by the queue to a <strong><em>single thread across all instances</em></strong> of the web service.  How would I best go about setting up a single queue to be accessed by any instance of the web service?</p>