MS SQL Server 2005 で実行されているデータベースと ASP.NET 3.5 Web アプリケーションがあります。
データベースには、Web アプリで実行されている CMS に「ページ」をフィードするために使用している製品カタログが含まれています。ページが作成されると、アプリケーションはページをキャッシュするので、この変更をアプリケーションに通知して、ページ オブジェクトを再作成できるようにする必要があります。
CMS は独自のキャッシュを使用するため、SqlCacheDependency を使用してこのタスクを実行することはできません。
アプリを起動すると、サブスクライバーが結果に表示されます
select * from sys.dm_qn_subscriptions
しかし、テーブルにデータを追加するとすぐに、サブスクライバーが消え、OnChanged イベントが発生しなくなります。
私のスタートアップコードには次のものがあります
// Ensure the database is setup for notifications
SqlCacheDependencyAdmin.EnableNotifications(DataHelper.ConnectionString);
SqlCacheDependencyAdmin.EnableTableForNotifications(DataHelper.ConnectionString, "Category");
SqlCacheDependencyAdmin.EnableTableForNotifications(DataHelper.ConnectionString, "Product");
if (!SqlDependency.Start(DataHelper.ConnectionString, SqlDependencyQueueName))
throw new Exception("Something went wrong");
string queueOptions = string.Format("service = {0}", SqlDependencyQueueName);
using (var connection = new SqlConnection(DataHelper.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [CategoryID],[Name]
FROM [dbo].[Category]", connection))
{
// Create a dependency and associate it with the SqlCommand.
dependency = new SqlDependency(command, queueOptions, 0);
// Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
command.ExecuteReader().Close();
}
}
// ...
void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
Debugger.Break(); // This never hits
this.ReloadData();
}