Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ^_^

I want to watch a Configuration table for any changes. This table essentially contains user settings that can be over-ridden by an Admin. If some functionality or access is turned off, then I need to alert the client side (using web sockets).

The issue is that the Admin app and Client app are not in the same solution.

My colleague wants to poll my db (he loves his polls >_<) but I would rather something more elegant.

The Configurations won't be changed very often at all.

So, here is what I have in my Context Ctor:


C#
public Context() // custom Context ctor.
            : base(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString)
  {
    SqlDependency.Stop(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString);
    SqlDependency.Start(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString);

     SqlDependency dep = new SqlDependency((SqlCommand) GetCommand(Configurations.AsQueryable()));

    dep.OnChange += (sender, args) =>
    {
      ConfigurationView.Changed(sender, args);
    };

  }


I have the server agent running and I've enable the broker on the database, but when I change a db value via MSSqlMS, the events don't fire.

Have I completely misunderstood the usage?

Any hints would be helpful ^_^
Thanks
Andy

What I have tried:

I was hoping to use the L2S On[prop]Changing / Changed events, but I gather that they are just PropertyChanged methods within the Context instance.
Posted
Updated 23-Jan-17 8:31am

1 solution

It looks like you're missing a vital step:

5. Execute the command using any of the Execute methods of the SqlCommand object. Because the command is bound to the notification object, the server recognizes that it must generate a notification, and the queue information will point to the dependencies queue.

In other words, the notification isn't registered until the associated command is executed. Since you never execute the command, the notification isn't registered, and the change event is never raised.
using (SqlCommand command = (SqlCommand)GetCommand(Configurations.AsQueryable()))
{
    SqlDependency dep = new SqlDependency(command);
    
    dep.OnChange += (sender, args) =>
    {
      ConfigurationView.Changed(sender, args);
    };
    
    using (SqlDataReader reader = command.ExecuteReader())
    {
        // You might need to read the rows here; the documentation isn't clear.
    }
}
 
Share this answer
 
Comments
Andy Lanng 24-Jan-17 9:47am    
Ah! Seems so simple now #^_^#

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900