Question

I'm having trouble with the implementation of SqlDependency in my project.

I'm using SqlDependency in a WCF Service. WCF Service then holds in memory cache all results from all tables in order to have a huge speed gain. Everything seems to be working fine, except when I'm doing a table row update. If I add or delete a row in my table, DataContext is refreshed and cache is invalidated without problems. But when it comes to a table row update, nothing happens, the cache is not invalidated and when I look in debug mode at the content of DataContext, no changes seems to be there.

Here's the code I'm using (note that I'm using the System.Runtime.Caching object) :

public static List<T> LinqCache<T>(this Table<T> query) where T : class
        {
            ObjectCache cache = MemoryCache.Default;

            string tableName = 
                query.Context.Mapping.GetTable(typeof(T)).TableName;
            List<T> result = cache[tableName] as List<T>;

            if (result == null)
            {
                using (SqlConnection conn = 
                    new SqlConnection(query.Context.Connection.ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(
                        query.Context.GetCommand(query).CommandText, conn);
                    cmd.Notification = null;
                    cmd.NotificationAutoEnlist = true;

                    SqlDependency dependency = new SqlDependency(cmd);
                    SqlChangeMonitor sqlMonitor = 
                        new SqlChangeMonitor(dependency);

                    CacheItemPolicy policy = new CacheItemPolicy();
                    policy.ChangeMonitors.Add(sqlMonitor);

                    cmd.ExecuteNonQuery();
                    result = query.ToList();
                    cache.Set(tableName, result, policy);
                }
            }
            return result;
        }

I created an extension method so all I have to do is to query any table like that :

List<MyTable> list = context.MyTable.LinqCache();

My DataContext is opened at the Global.asax Application_OnStart and stored in cache, so I can use it whenever I want in my WCF Service. As well at this moment I'm opening the SqlDependency object with

SqlDependency.Start(
    ConfigurationManager.ConnectionStrings[myConnectionString].ConnectionString);

So, is that a limitation of SqlDependency, or I'm doing something wrong/missing something in the process?

Was it helpful?

Solution

I think the problem is that although you do all the work in setting up the command object you then do:

cmd.ExecuteNonQuery();
result = query.ToList();

Which is going to use your SQL Command and throw away the results then LINQ to SQL will generate it's own internally via query.ToList(). Thankfully you can ask LINQ to SQL to execute your own command and translate the results for you so try replacing those two lines with:

results = db.Translate<T>(cmd.ExecuteReader());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top