SqlDependency事件未触发

时间:2011-10-21 16:20:17

标签: c# .net sql-server-2008 service-broker

许多人似乎在做这项工作时遇到了问题,所以我不是新来的,希望有人能够指出我还能检查什么,这就是我在做什么......

环境:.net 4.0, SQL Server Exrpress 2008 R2

设置数据库的步骤

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

以下代码测试通知

     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 = "test@test.com", 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);
            }
        }

当我在sql server上的查询窗口中的电子邮件列上运行更新语句时,我没有看到更改事件被触发。

事件日志或数据库日志中没有事件,当我运行以下查询时,我什么也看不见

select * from WebSiteCacheMessages -- queue name
select * from sys.transmission_queue
非常感谢您阅读此内容......

1 个答案:

答案 0 :(得分:3)

您实际上并没有使用SqlDependency执行任何命令。您只需将SqlDependency与SqlCommand相关联,但实际上并不是执行 SqlCommand。只有在执行SQL语句时才会创建服务器上的查询通知。类似的东西:

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 ();

另一种方法是使用LinqToCache并查询您的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 ) {
 ....
}