title: Thread Safe Publish Subscribe in .Net
body: <p>I created a simple set of interfaces and a class that allows me to publish the additions and removals of items in a generic dictionary. Subscribers receive the entire list when they subscribe and after that, they get only the changes.</p>
  
  <p>Although my solutions work, I am looking for something a little more standard, a little less home-grown. Do you have any suggestions?</p>
  
  <p><strong>Notes on what I've found so far:</strong></p>
  
  <p>I have been looking at Microsoft's Reactive Extensions (Rx). According to Jon Skeet's article "LINQ to Rx: second impressions" [1], he says "as soon as an observer subscribes, the observable publishes everything in the sequence to it (on a different thread, by default). Separate calls to Subscribe make the observable iterate over the sequence multiple times." This sounds like the basic idea, but I can't find any specific examples and I'm not really sure about the thread-safety of "Subject" or "AsyncSubject" yet.</p>
  
  <p><strong>Notes on my home-grown solution:</strong></p>
  
  <p>The structure that is delivered to subscribers looks like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interface for a set of changes that are being published.
  /// &lt;/summary&gt;
  /// &lt;typeparam name="TKey"&gt;&lt;/typeparam&gt;
  /// &lt;typeparam name="TItem"&gt;&lt;/typeparam&gt;
  public interface IPublication&lt;TKey, TItem&gt;
  {
      /// &lt;summary&gt;
      /// Version of the list.
      /// &lt;/summary&gt;
      long Version { get; }
      /// &lt;summary&gt;
      /// Items that were added or updated.
      /// &lt;/summary&gt;
      IEnumerable&lt;TItem&gt; ChangedItems { get; }
      /// &lt;summary&gt;
      /// Keys to items that were removed.
      /// &lt;/summary&gt;
      IEnumerable&lt;TKey&gt; RemovedKeys { get; }
  }
  </code></pre>
  
  <p>The subscribers themselves must implement this interface:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interface for a subscriber that will receive IPublication{TKey, TItem} deliveries from a publisher.
  /// &lt;/summary&gt;
  /// &lt;typeparam name="TKey"&gt;&lt;/typeparam&gt;
  /// &lt;typeparam name="TItem"&gt;&lt;/typeparam&gt;
  public interface ISubscribe&lt;TKey, TItem&gt;
  {
      void Deliver(IPublication&lt;TKey, TItem&gt; pub);
  }
  </code></pre>
  
  <p>And of course my generic dictionary publisher class has this method:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Adds the give subscriber to the list of subscribers and immediately publishes the
  /// dictionary contents to the new subscriber. The return value may be disposed when
  /// the subscriber wishes to terminate it's subscription.
  /// &lt;/summary&gt;
  /// &lt;param name="subscriber"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public IDisposable Subscribe(ISubscribe&lt;TKey, TItem&gt; subscriber);
  </code></pre>
  
  <p>[1]
  <a href="https://codeblog.jonskeet.uk/2010/01/19/linq-to-rx-second-impressions/" rel="nofollow noreferrer">https://codeblog.jonskeet.uk/2010/01/19/linq-to-rx-second-impressions/</a></p>