Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 信号器SqlDependency OnChange事件未触发_Asp.net Mvc_Model View Controller_Push Notification_Signalr - Fatal编程技术网

Asp.net mvc 信号器SqlDependency OnChange事件未触发

Asp.net mvc 信号器SqlDependency OnChange事件未触发,asp.net-mvc,model-view-controller,push-notification,signalr,Asp.net Mvc,Model View Controller,Push Notification,Signalr,我在MVC中使用SignalR,并创建SqlDependency的更改事件。但当我在数据库中更新时,它不会触发。当我在Db中执行查询“ALTERDATABASE MyDatabase SET ENABLE_BROKER” 我用NotificationComponent写的 public void RegisterNotification(DateTime currentTime) { string conStr = ConfigurationManager.Con

我在MVC中使用SignalR,并创建SqlDependency的更改事件。但当我在数据库中更新时,它不会触发。当我在Db中执行查询“ALTERDATABASE MyDatabase SET ENABLE_BROKER”

我用NotificationComponent写的

    public void RegisterNotification(DateTime currentTime)
    {
        string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
        string sqlCommand = @"SELECT * from [dbo].[Event] where UpdatedTime > @UpdatedTime";
        SqlDependency.Start(conStr);

        using (SqlConnection con = new SqlConnection(conStr))
        {
            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }

            SqlCommand cmd = new SqlCommand(sqlCommand, con);
            cmd.Parameters.AddWithValue("@UpdatedTime", currentTime);

            cmd.Notification = null;
            SqlDependency sqlDep = new SqlDependency(cmd);

            //sqlDep.OnChange += new OnChangeEventHandler(sqlDep_OnChange);
            sqlDep.OnChange += sqlDep_OnChange;
            //we must have to execute the command here
            
            cmd.ExecuteReader().Dispose();
            //using (SqlDataReader reader = cmd.ExecuteReader())
            //{
            //    // nothing need to add here now
            //}

        }
    }

    public void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change)
        {
            SqlDependency sqlDep = sender as SqlDependency;
            sqlDep.OnChange -= sqlDep_OnChange;

            //from here we will send notification message to client
            var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
            notificationHub.Clients.All.notify("added");
            
            //re-register notification
            RegisterNotification(DateTime.Now);
        }
    }

    public List<Event> GetEvents(DateTime afterDate)
    {
        using (MyDatabaseEntities dc = new MyDatabaseEntities())
        {
            return dc.Events.Where(a => a.UpdatedTime > afterDate).OrderByDescending(a => a.UpdatedTime).ToList();
        }
    }
在Startup.cs配置功能中:


app.mapsigner()

sql Dependency*符号在查询中不起作用,所以我们需要在查询中写入字段名称。但现在,当我在不同浏览器中打开站点的2个实例,然后它运行函数2次时,又出现了1个问题。并将计数计算为2。对于每个浏览器,它应该为1。所以请帮我解决这个问题
        var notificationHub = $.connection.notificationHub;
       
       

        $.connection.hub.start().done(function () {
            console.log('Notification hub started');
        });

         notificationHub.client.notify = function (message) {
            if (message && message.toLowerCase() == "added") {
                updateNotificationCount();
            }
        }