【问题标题】:C# SqlCacheDependency - Should I have to EnableNotifications and EnableTableForNotifications Whenever my application is ran?C# SqlCacheDependency - 每当我的应用程序运行时,我是否必须启用通知和 EnableTableForNotifications?
【发布时间】:2017-03-31 17:54:47
【问题描述】:
我想知道是否必须在运行我的应用程序时执行此代码,或者一旦我运行它,我的数据库现在设置为在确定的指定表发生更改时清理我的缓存。
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(cs);
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(cs, "TABLENAME");
【问题讨论】:
标签:
c#
caching
sqldependency
【解决方案1】:
你可以在设置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.