title: Simple in-memory message queue
body: <p>Our existing implementation of domain events limits (by blocking) publishing to one thread at a time to avoid reentrant calls to handlers:</p>
  
  <pre><code>public interface IDomainEvent {}  // Marker interface
  
  public class Dispatcher : IDisposable
  {
      private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
  
      // Subscribe code...
  
      public void Publish(IDomainEvent domainEvent)
      {
          semaphore.Wait();
          try
          {
              // Get event subscriber(s) from concurrent dictionary...
  
              foreach (Action&lt;IDomainEvent&gt; subscriber in eventSubscribers)
              {
                  subscriber(domainEvent);
              }
          }
          finally
          {
              semaphore.Release();
          }
      }
      // Dispose pattern...
  }
  </code></pre>
  
  <p>If a handler publishes an event, this will deadlock.</p>
  
  <p>How can I rewrite this to serialize calls to <code>Publish</code>? In other words, if subscribing handler A publishes event B, I'll get:</p>
  
  <ol>
  <li>Handler A called</li>
  <li>Handler B called</li>
  </ol>
  
  <p>while preserving the condition of no reentrant calls to handlers in a multithreaded environment.</p>
  
  <p>I do not want to change the public method signature; there's no place in the application to call a method to publish a queue, for instance.</p>