Reputation: 5815
many people seem to have problem in making this work, so i am not new to here, hoping someone would be able to point out what else i can check, this is what i am doing...
Environment : .net 4.0, SQL Server Exrpress 2008 R2
Steps taken to setup the database
CREATE QUEUE WebSiteCacheMessages ;
CREATE SERVICE WebCacheNotifications ON QUEUE WebSiteCacheMessages
([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]) ;
CREATE ROUTE WebCacheMessagesRoute WITH SERVICE_NAME = 'WebCacheNotifications',ADDRESS = 'LOCAL' ;
EXEC sp_configure 'show advanced options', '1'
GO
RECONFIGURE
GO
EXEC sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO
ALTER DATABASE EFTest SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
GO
ALTER AUTHORIZATION ON DATABASE::[EFTEST] TO [domain\user];
GO
Following code to test the notification
static void Main(string[] args)
{
var id = 0;
string sqlStatement = "SELECT [Id] ,[FirstName],[LastName],[Email] FROM [dbo].[People]";
string queueName = "WebSiteCacheMessages";
string connectionString = ConfigurationManager.ConnectionStrings["MyDataContext"].ConnectionString;
try
{
SqlDependency.Start(connectionString, queueName);
using (var db = new DataContext(connectionString))
{
var person = new Person { Email = "[email protected]", FirstName = "Testy", LastName = "Tester", HomeAddress = address };
db.Persons.Add(person);
db.SaveChanges();
id = person.Id;
}
var dependency = new SqlDependency();
dependency.AddCommandDependency(new SqlCommand(sqlStatement));
dependency.OnChange += (o, e) =>
{
Console.WriteLine("Notification called !");
};
Console.ReadLine();
}
finally
{
SqlDependency.Stop(connectionString, queueName);
}
}
when i run an update statement on the email column in query window on sql server, i dont see on change event being fired.
There are no events in the event log or database logs, and when i run the following query, i dont see anything
select * from WebSiteCacheMessages -- queue name
select * from sys.transmission_queue
many thanks for reading through this...
Upvotes: 1
Views: 6389
Reputation: 294307
You don't actually execute any command with a SqlDependency on it. You simply associate the SqlDependency with a SqlCommand, but you don't actually execute the SqlCommand. The Query Notification on server gets created only when you execute the SQL statement. Something like:
var dependency = new SqlDependency();
var SqlCommand cmd = new SqlCommand(sqlStatement);
dependency.AddCommandDependency(cmd);
dependency.OnChange += (o, e) =>
{
Console.WriteLine("Notification called !");
};
// Executing the command will submit the query notification request
using (SqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Reader ()) {
...
}
}
Console.Reade ();
Another alternative is to use LinqToCache and query your DataContext.Persons
:
var people = from p in db.Persons select new {p.Id, p.LastName, p.FirstName, p.Email};
var peopleCached = people.AsCached("Persons", new QueryCachedOptions () {
OnInvalidate = (sender, args) => {
Console.WriteLine("Notification called !");
}
});
// Again, the underlying SqlCommand must actually be executed. Iterate the query
foreach (p in peopleCached ) {
....
}
Upvotes: 3