Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7487895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:41:03+00:00 2026-05-29T14:41:03+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:41:03+00:00Added an answer on May 29, 2026 at 2:41 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.