Click here to Skip to main content
15,889,595 members
Articles / Desktop Programming / WPF

AutoRefresh Entity Framework Data Using SQL Server Service Broker

Rate me:
Please Sign up or sign in to vote.
4.77/5 (35 votes)
14 Sep 2012CPOL3 min read 167.7K   3.8K   95   60
How to use SqlDependency to refresh Entity Framework Object Sets automatically on DB changes

Automatically Update Your Application on DB Changes

Starting with SQL Server 2005, Microsoft introduced a new feature called SQL Server Service Broker (SSB) that allows communications (and notifications!) from SQL Server to other places. There is a class in the .NET Framework called SqlDependency that allows to notify the client about database changes. To get this to work, the SqlDependency constructor needs a SqlCommand - and that's a problem when you're using Entity Framework. The SqlCommand objects are hidden deep inside EF's objects.

Some time ago, a great post from Fritz Onion (previously published at http://www.pluralsight-training.net/community/blogs/fritz/archive/2006/06/15/27694.aspx) revealed a "magic cookie" that allows to connect a SqlDependency object to a hidden SqlCommand. This technique has been used in the open source project LinqToCache that maintains a cache using LINQ to SQL.

In this article, I'll introduce a way to use SQL Server Service Broker notifications in order to automatically update an Entity Framework object set.

The AutoRefresh Extension Method

You'll probably be familiar with the Refresh method that allows you to update an object set (a representation of a table) in your object context with fresh data from your database:

C#
this.MyObjectContext.Refresh(RefreshMode.StoreWins, this.MyObjectContext.MyObjectSet);

Using the ServiceBrokerUtility outlined below, it's easy to refresh your object set not only once, but automatically on each update that occurs in the database. All you need is to replace the Refresh method by the new AutoRefresh extension method defined in ServiceBrokerUtility.

C#
this.MyObjectContext.AutoRefresh
	(RefreshMode.StoreWins, this.MyObjectContext.MyObjectSet);

The ServiceBrokerUtility

Here's the code for all that magic (coming out of the cookie):

C#
public static class ServiceBrokerUtility
{
  private static List<string> connectionStrings = new List<string>();
  private const string sqlDependencyCookie = "MS.SqlDependencyCookie";
  private static ObjectContext ctx;
  private static RefreshMode refreshMode;
  private static readonly Dictionary<string, IEnumerable> collections = new Dictionary<string, IEnumerable>();

  static public void AutoRefresh(this ObjectContext ctx, 
                RefreshMode refreshMode, IEnumerable collection)
  {
    var csInEF = ctx.Connection.ConnectionString;
    var csName = csInEF.Replace("name=", "").Trim();
    var csForEF = 
      System.Configuration.ConfigurationManager.ConnectionStrings
		[csName].ConnectionString;
    var newConnectionString = new 
      System.Data.EntityClient.EntityConnectionStringBuilder
			(csForEF).ProviderConnectionString;
    if (!connectionStrings.Contains(newConnectionString))
    {
      connectionStrings.Add(newConnectionString);
      SqlDependency.Start(newConnectionString);
    }
    ServiceBrokerUtility.ctx = ctx;
    ServiceBrokerUtility.refreshMode = refreshMode;
    AutoRefresh(collection);
  }

  static public void AutoRefresh(IEnumerable collection)
  {
   var oldCookie = CallContext.GetData(sqlDependencyCookie);
   try
   {
    var dependency = new SqlDependency();
    collections.Add(dependency.Id, collection);
    CallContext.SetData(sqlDependencyCookie, dependency.Id);
    dependency.OnChange += dependency_OnChange;
    ctx.Refresh(refreshMode, collection);
   }
   finally
   {
    CallContext.SetData(sqlDependencyCookie, oldCookie);
   }
  }

  static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
  {
    if (e.Info == SqlNotificationInfo.Invalid)
    {
      Debug.Print("SqlNotification:  A statement was provided that cannot be notified.");
      return;
    }
    try{
      var id =((SqlDependency)sender).Id;
      IEnumerable collection;
      if (collections.TryGetValue(id, out collection))
      {
        collections.Remove(id);
        AutoRefresh(collection);
        var notifyRefresh = collection as INotifyRefresh;
        if (notifyRefresh != null)
          System.Windows.Application.Current.Dispatcher.BeginInvoke(
            (Action)(notifyRefresh.OnRefresh));
      }
    }
    catch (Exception ex)
    {
      System.Diagnostics.Debug.Print("Error in OnChange: {0}", ex.Message);
    }
  }
}

Almost Ready to Use

Isn't that fancy? Once I'd developed that code, I instantly bound my (now automatically updated) object set to the ItemsSource property of a DataGrid and pressed F5 in Visual Studio in order to compile and run my program. I displayed Visual Studio's Server Explorer, right-clicked on my table, selected "Show Table Data" and thus changed the value of a field in the database.

Tadaa - the field's value changed in my program's grid accordingly! After I'd enjoyed that game some time I tried to add a new record to the database. Disappointingly my grid didn't honor the new record in any way.

The debugger revealed that new records arrived as soon as they were created in my object set, but the DataGrid didn't care. After some research, I found that (for whatever reason) the object sets are lacking any support of the INotifyCollectionChanged interface.

New problems - ok, we need more code.

The AutoRefreshWrapper

This class takes an object set and spits out a typed enumerable with suitable notification support for the controls and of course with ServiceBrokerUtility's autorefresh power.

Using this wrapper class, we can easily implement a property in a view model that we can use to bind to:

C#
public class MyViewModel
{
    public MyViewModel()
    {
        ..
        MyThings = new AutoRefreshWrapper<thing>(
              MyObjectContext.MyObjectSet, RefreshMode.StoreWins);
        ..
    }
 
    public IEnumerable<thing> MyThings { get; private set; }
 
    ..
}

Here is the wrapper class:

C#
public class AutoRefreshWrapper<T> : IEnumerable<T>, INotifyRefresh
    where T : EntityObject
{
    private IEnumerable<T> objectQuery;
    public AutoRefreshWrapper(ObjectQuery<T> objectQuery, RefreshMode refreshMode)
    {
        this.objectQuery = objectQuery;
        objectQuery.Context.AutoRefresh(refreshMode, this);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return objectQuery.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

    public void OnRefresh()
    {
        try
        {
            if (this.CollectionChanged != null)
                CollectionChanged(this, 
                  new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Print("Error in OnRefresh: {0}", ex.Message);
        }
    }

    public event NotifyCollectionChangedEventHandler CollectionChanged;
}

This class implements a new interface INotifyRefresh:

C#
public interface INotifyRefresh : INotifyCollectionChanged
{
	void OnRefresh();
}

Finally, that whole solution worked fine for me. But be careful - there are some restrictions regarding the queries that are working with Service Broker. And finally: The "Magic Cookie" is an undocumented feature, thus you should take care.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: multiple datasets Pin
Harry von Borstel3-Sep-12 22:38
Harry von Borstel3-Sep-12 22:38 
GeneralMy vote of 5 Pin
Christian Amado3-Aug-12 7:57
professionalChristian Amado3-Aug-12 7:57 
GeneralInvalid url Pin
Sergey Morenko2-Aug-12 7:38
professionalSergey Morenko2-Aug-12 7:38 
GeneralRe: Invalid url Pin
Harry von Borstel2-Aug-12 20:32
Harry von Borstel2-Aug-12 20:32 
GeneralMy vote of 5 Pin
mrmiagi010125-Jul-12 23:12
mrmiagi010125-Jul-12 23:12 
GeneralOnChange is called endless Pin
mrmiagi010125-Jul-12 2:03
mrmiagi010125-Jul-12 2:03 
GeneralMy vote of 4 Pin
chang3d20-Jun-12 12:27
chang3d20-Jun-12 12:27 
QuestionJournal of how I used this... and where I failed... Pin
chang3d18-Jun-12 13:50
chang3d18-Jun-12 13:50 
I put break points everywhere, and it stops at the following line:
C#
ctx.Refresh(refreshMode, collection);


I tried to enumerate the collection during debug, and it said function timed out. I added Connect Timeout = 30 to the connection string, and it gave me some kind of error that I cannot replicate. (Update: It's a "ContextSwitchDeadlock" MDA error. So I've unchecked the debugger exception.) I ran it again, and after an awfully long time (like over 3 minutes), my table finally loaded. My table has more than 300000 rows, so I'm think that may have something to do with the slowness. (But if I commented out the AutoRefresh line, my program loads in less than 10 seconds.)

In my cs file:
C#
using (var entity = new Entities())
{
    entity.AutoRefresh(RefreshMode.StoreWins, entity.table);
    dataGrid.ItemsSource = entity.table;
}


In my xaml:
XML
<DataGrid Name="dataGrid" IsReadOnly="True" ItemsSource="{Binding}" />


After things are loaded, I'm not seeing any OnChange event fired even though my table has had hundreds of inserts. So I re-read the article and finally realized what this article is called AutoRefresh... (whoops)

So I changed what I had in my cs file to this:
C#
using (var entity = new Entities())
{
    var source = new AutoRefreshWrapper<table>(entity.table, RefreshMode.StoreWins);
    dataGrid.ItemsSource = source;
}


And sadly, this is where I'm lost. It still doesn't auto refresh. No event was fired. What am I doing wrong here? Update: I tried this with another database. And it's working perfectly. The original db that I tried it with must be screwed up (they are set up completely in the same way). Everything works. (I hope those people who can't figure out how to use this can now.)

And thank you for this code.

modified 20-Jun-12 18:25pm.

QuestionHow would you modify this code for EF 4.3 using DbContext Pin
Tim Elvidge7-May-12 12:48
Tim Elvidge7-May-12 12:48 
AnswerRe: How would you modify this code for EF 4.3 using DbContext Pin
Harry von Borstel7-May-12 21:57
Harry von Borstel7-May-12 21:57 
GeneralRe: How would you modify this code for EF 4.3 using DbContext Pin
Tim Elvidge8-May-12 18:55
Tim Elvidge8-May-12 18:55 
GeneralRe: How would you modify this code for EF 4.3 using DbContext Pin
Member 831284830-Aug-12 11:53
Member 831284830-Aug-12 11:53 
GeneralRe: How would you modify this code for EF 4.3 using DbContext Pin
Tim Elvidge30-Aug-12 12:29
Tim Elvidge30-Aug-12 12:29 
GeneralRe: How would you modify this code for EF 4.3 using DbContext Pin
Member 831284831-Aug-12 8:17
Member 831284831-Aug-12 8:17 
GeneralMy vote of 2 Pin
Gusteru_2023-Apr-12 21:22
Gusteru_2023-Apr-12 21:22 
GeneralRe: My vote of 2 Pin
Harry von Borstel26-Apr-12 2:21
Harry von Borstel26-Apr-12 2:21 
GeneralMy vote of 1 Pin
sangchi.rk26-Mar-12 3:09
sangchi.rk26-Mar-12 3:09 
QuestionReg: downloading of original Code. Pin
sangchi.rk26-Mar-12 2:41
sangchi.rk26-Mar-12 2:41 
AnswerRe: Reg: downloading of original Code. Pin
Harry von Borstel1-Apr-12 5:52
Harry von Borstel1-Apr-12 5:52 
SuggestionNeed download Pin
gs.meena9-Aug-11 1:35
gs.meena9-Aug-11 1:35 
GeneralRe: Need download Pin
Harry von Borstel9-Aug-11 21:02
Harry von Borstel9-Aug-11 21:02 
GeneralRe: Need download Pin
Michael90002-Nov-11 5:22
Michael90002-Nov-11 5:22 
AnswerRe: Need download Pin
Harry von Borstel2-Nov-11 7:36
Harry von Borstel2-Nov-11 7:36 
GeneralRe: Need download Pin
smurancsik20-Feb-12 7:12
smurancsik20-Feb-12 7:12 
AnswerRe: Need download Pin
Harry von Borstel5-Sep-12 5:36
Harry von Borstel5-Sep-12 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.