SqlDependancy触发次数更多

时间:2015-06-10 07:32:49

标签: sql asp.net signalr sqldependency

您好我正在尝试使用signalr实现sqldependancy以进行新的数据库更改。但问题是如果我只编辑一行,那么sqldependancy也会激发更多时间。有一段时间没有开火。我有没有MVC的Web应用程序。吼叫是我的代码

 public void page_load(object sender, EventArgs e)
        {
            //notification.GetAllUnreadSalesNotifications();
            NewScrapNotifications();

        }



        public void NewScrapNotifications()
        {


            string message = string.Empty;

            using (SqlConnection connection = new SqlConnection(IFTDAL.IFTCommon.DSN))
            {
                string query = "SELECT [Message] FROM [dbo].[DummyData]";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Notification = null;
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        message = reader[0].ToString();
                    }
                }
            }
        }



        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {

            if (e.Type == SqlNotificationType.Change)
            {
                IFTHub nHub = new IFTHub();
                nHub.NotfiyAllClients();
               // NewScrapNotifications();
                //LoadDashboardByDashboardViewID(1);
            }
        }

我在Globa.asax中也有以下代码

 void Application_Start(object sender, EventArgs e)
            {
                // Code that runs on application startup
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                SqlDependency.Start(DAL.DSN);

            }
            protected void Application_End(object sender, EventArgs e)
            {
                SqlDependency.Stop(DAL.DSN);
            }

1 个答案:

答案 0 :(得分:3)

您正在为每个页面加载(刷新)创建新的SQLDependency。我敢打赌,页面重新加载次数与通过SignalR接收的通知数量之间存在相关性。

每个应用生命周期仅注册一次通知...

相关问题