【问题标题】:SqlDependency not firingSqlDependency 未触发
【发布时间】:2017-09-26 19:49:15
【问题描述】:

我是第一次尝试 SqlDepenedency。我没有收到有关数据库更新的任何通知。

我在里面放置断点: OnChange(object sender, SqlNotificationEventArgs e),

但它永远不会被击中。

这是我的代码:

    protected void Page_Load(object sender, EventArgs e)
    {

        Label1.Text = "Cache Refresh: " + DateTime.Now.ToLongTimeString();
        DateTime.Now.ToLongTimeString();
        // Create a dependency connection to the database.
        SqlDependency.Start(GetConnectionString());

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            using (SqlCommand command = new SqlCommand(GetSQL(), connection))
            {

                SqlDependency dependency =
                        new SqlDependency(command);

                    // Refresh the cache after the number of minutes
                    // listed below if a change does not occur.
                    // This value could be stored in a configuration file.
                                  connection.Open();

                dgHomeRequests.DataSource = command.ExecuteReader();
                    dgHomeRequests.DataBind();

            }
        }   
    }


    private string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file.
        //return "Data Source=(local);Integrated Security=true;" +"Initial Catalog=AdventureWorks;";
        return ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
    }
    private string GetSQL()
    {
        return "Select [Address] From [UserAccount1]";
    }


    void OnChange(object sender, SqlNotificationEventArgs e)
    {
        // have breakpoint here:
        SqlDependency dependency = sender as SqlDependency;

        // Notices are only a one shot deal
        // so remove the existing one so a new 
        // one can be added

        dependency.OnChange -= OnChange;

        // Fire the event
       /* if (OnNewMessage != null)
        {
            OnNewMessage();
        }*/
    }

我还在 Global.asax 文件中放置了一些代码:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        SqlDependency.Start(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
    }
    protected void Application_End()
    {
        // Shut down SignalR Dependencies
        SqlDependency.Stop(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
    }
}

SQL 服务器在本地机器上。我正在通过 Visual Studio(IIS Express) 运行代码。

  1. 在数据库上启用服务代理:

    ALTER DATABASE SET ENABLE_BROKER 去吧

  2. 要订阅查询通知,我们需要授予 IIS 服务帐户权限

    GRANT SUBSCRIBE QUERY NOTIFICATIONS TO “<serviceAccount>”
    

我猜第二点不需要,因为它是本地的。但我试着给它一些权限。不知道他们是否正确,因为我认为它没有使用应用程序池。并且不需要本地环境的许可。如果我自己是用户并自己创建了架构。

我看到的一个问题是:

alter authorization on database::<dbName> to [sa];

我也给予了许可。

【问题讨论】:

  • 您能否检查两件事:1) 通知已“设置”,请参阅sys.dm_qn_subscriptions 和 2) 通知消息未保留在 sys.transmission_queue
  • @RemusRusanu 我查看了视图,为 sys.dm_qn_subscriptionssys.transmission_queue 选择了前 1000 行。他们都在那里,他们都是空的。有什么异常吗?
  • sys.qn_subscriptions 中应该有一行。我怀疑您的查询通知会立即失效,因为它违反了here 列出的限制之一。您可以使用基本的SqlDependency 而不是SqlCacheDependency 并检查您在SqlNotificationEventArgs 中获得的InfoSourceType
  • 我在sys.dm_qn_subscriptions 中有一行,只要我在我的表中插入一些东西,sys.dm_qn_subscriptions 中的行就消失了!所以我希望查询通知不会失效..我现在使用您所说的链接中提到的两部分 dbName。
  • 除了我上面的评论,因为我是第一次这样做,你能检查我的 OnChange 函数是否正确,如果需要任何额外的步骤,是否应该启动数据库更改?因为我确实看到在我的应用程序洞察中创建了一个依赖项。

标签: sql asp.net webforms sqldependency


【解决方案1】:

我失踪了: dependency.OnChange += new OnChangeEventHandler(OnChange); 新代码如下所示:

               SqlDependency dependency = new SqlDependency(command);

               dependency.OnChange += new OnChangeEventHandler(OnChange);

现在我可以开火了 void OnChange(object sender, SqlNotificationEventArgs e)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多