title: Why am I getting an 'open data reader' exception with my SqlDependency subscription?
body: <p>I have finally, with an aggregate of an amazingly high numbers of dysfunctional examples, managed to receive change messages on a <code>SqlDependency</code> object. This knowledge may prepare you for the inelegance or plain incorrectness of my code.</p>
  
  <p>I have an <code>ActiveList&lt;T&gt;: ObservableCollection&lt;T&gt;</code> class that listens for changes to it's DB table data source and repopulates itself. I use the following code to create and initialize the list and the <code>SqlDependency</code>:</p>
  
  <p>Constructor:</p>
  
  <pre><code>public ActiveList()
  {
      PopulateList();
  }
  </code></pre>
  
  <p>Populate:</p>
  
  <pre><code>private void PopulateList()
  {        Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, ((Action)(() =&gt;
      {
          Clear();
          using (var dbContext = new XTimeDbContext())
          {
              var set = dbContext.Set&lt;T&gt;().ToList();
              this.AddRange(set);
          }
      })));
      SubscribeNotifications();
  }
  </code></pre>
  
  <p>Subscribe:</p>
  
  <pre><code>private void SubscribeNotifications()
  {
      const string dependencyQuery = "select TITLE_ACTIVE, TITLE_NAME from dbo.TITLE";
      var dependency = new SqlDependency();
      dependency.OnChange += DependencyOnChange;
      var connectionString = _dbContext.Database.Connection.ConnectionString;
  
      //SqlDependency.Stop(connectionString);
      SqlDependency.Start(connectionString);
      using (var sqn = new SqlConnection(connectionString))
      {
          sqn.Open();
          using (var cmd = new SqlCommand(dependencyQuery, sqn))
          {
              cmd.Notification = null;
              dependency.AddCommandDependency(cmd);
              //dependency.OnChange += DependencyOnChange;
              using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
              {
                  while (reader.Read())
                  {
                  }
              }                
          }
      }
  }
  </code></pre>
  
  <p>And, finally, listen:</p>
  
  <pre><code>private void DependencyOnChange(object sender, SqlNotificationEventArgs sqlNotificationEventArgs)
  {
      _trace.TraceInformation("DependencyOnChange called. Reason: '{0}', Source: '{1}', Type: '{2}'.", sqlNotificationEventArgs.Info, sqlNotificationEventArgs.Source,
          sqlNotificationEventArgs.Type);
      //if (!_isPopulating)
      //{
      //    PopulateList();
      //}
      SqlDependency.Stop(_dbContext.Database.Connection.ConnectionString;);
      SubscribeNotifications();
      _trace.TraceInformation("DependencyOnChange completed.");
  }
  </code></pre>
  
  <p>The code is in slight disarray due to huge amounts of small, experimental changes, but my main problem is that when I run the test app that uses an <code>ActiveList</code>, I get the first change notification; my log shows "DependencyOnChange called". Then, the call to <code>SqlDependency.Stop</code>, wherever I place it, generates an <code>InvalidOperationException</code>, with the Message:</p>
  
  <pre><code>There is already an open DataReader associated with this Command which must be closed first.
  </code></pre>
  
  <p>I can find no 'dangling' DataReaders anywhere in my code, so what could be causing this? </p>