C# SqlCacheDependency - 每当我的应用程序为 运行 时,我是否必须启用 EnableNotifications 和 EnableTableForNotifications?

C# SqlCacheDependency - Should I have to EnableNotifications and EnableTableForNotifications Whenever my application is ran?

我想知道我是否必须在我的应用程序处于 运行 时执行此代码,或者一旦我 运行 它,我的数据库现在设置为在确定 table 时清理我的缓存] 指定已更改。

System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(cs); 

System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(cs, "TABLENAME");

在设置SqlCacheDependency的地方可以启用EnableNotifications和EnableTableForNotifications

不要忘记,如果你想禁用它,你将不得不调用 DisableNotifications 和/或 DisableTableForNotifications。

也就是说,您应该在 Application_Start 和 Application_End 方法中启动和停止侦听器。

protected void Application_Start(object sender, EventArgs e)
{  
    SqlDependency.Start("YOUR CONNECTION STRING");
}

protected void Application_End(object sender, EventArgs e)
{
    SqlDependency.Stop("YOUR CONNECTION STRING");
}

More info here.