为什么我的 SqlDependency 没有触发

发布于 2024-09-15 18:41:10 字数 1663 浏览 8 评论 0原文

我有一个在 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();
}

I have a database running on MS SQL Server 2005 and an ASP.NET 3.5 web application.

The database contains a product catalog which I'm using to feed "pages" into a CMS running in the web app. Once the pages have been created the application caches them so I need to notify the application of this change so it can recreate the page objects.

The CMS uses it's own caching so an SqlCacheDependency cannot be used to perform this task.

When I fire the app up I do get a subscriber appearing in the result of

select * from sys.dm_qn_subscriptions

But as soon as I add some data to the table, the subscriber disappears and the OnChanged event never fires.

In my startup code I have the following

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-09-22 18:41:10

检查您的数据库sys.transmission_queue。您的通知很可能会在那里,但由于无法发送而被保留。 transmission_status 将解释为什么会发生这种情况。有关更详细的故障排除指南,请参阅故障排除对话框

最常见的问题是由于孤立数据库 dbo 所有权无法满足 EXECUTE AS 基础设施要求,可以通过以下方式解决:

ALTER AUTHORIZATION ON DATABASE::<dbname> TO [sa];

但是,解决方案取决于具体情况,具体取决于实际问题,如上所述。

Check your database sys.transmission_queue. Most likely your notification(s) will be there, retained because they cannot be delivered. The transmission_status will have an explanation why is this happening. For a more detailed troubleshooting guide, see Troubleshooting Dialogs.

The most often issue is due to EXECUTE AS infrastructure requirements not being satisfied by an orphaned database dbo ownership and can be resolved via:

ALTER AUTHORIZATION ON DATABASE::<dbname> TO [sa];

however the solution depends from case to case, depending on the actual problem, as investigated above.

↘人皮目录ツ 2024-09-22 18:41:10

您的选择是错误的。您必须明确说明查询。
来自微软 SQL 文档:

必须显式声明 SELECT 语句中的投影列,并且表名称必须用两部分名称限定。请注意,这意味着语句中引用的所有表必须位于同一个数据库中。

Your Select is wrong. You must explicity state the Query.
From Microsoft SQL Docs:

The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-part names. Notice that this means that all tables referenced in the statement must be in the same database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文