SignalR sql依赖问题

时间:2017-03-31 19:05:21

标签: c# signalr sqldependency

我在我的应用程序中使用SignalR,使用了我所拥有的一个集线器:

public class MessagesHub : Hub
{
    [HubMethodName("getNotifications")]
    public static void GetNotifications()
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
        context.Clients.All.updateNotifications();
    }

   [HubMethodName("getChatMessages")]
    public static void GetChatMessages()
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
        context.Clients.All.updateChatMessages();
    }
}

我的signalR连接开始

$(function () {
    var notifications = $.connection.messagesHub;

    notifications.client.updateNotifications = function () {
        getNotifications();
    };

    notifications.client.updateChatMessages = function () {
        getChatMessages();
    };

    // Start the connection.
    $.connection.hub.start().done(function () {
        getNotifications();
        getChatMessages();
    }).fail(function (e) {
        alert(e);
    });
});

getNotifications()和getChatMessages()有ajax调用 - &gt; Controller - &gt; MessageRepository,它具有GetNotifications和GetMessages函数,如下所示:

 public NotificationsEntity GetNotifications(UserInfo user)
 {
    using (var connection = new SqlConnection(_connString))
    {
       connection.Open();
       using (var cmd = new SqlCommand(@"//sql query here", connection))
                {
                 command.Notification = null;
                 var notificationDependency = new SqlDependency(command);
                    notificationDependency.OnChange += new OnChangeEventHandler(dependency_OnChangeNotifications);

                 if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        ////reads the data
                    }
               }
       }
  }
  public NotificationsEntity GetMessages(ChatMessages msgs)
  {
    using (var connection = new SqlConnection(_connString))
    {
       connection.Open();
       using (var cmd = new SqlCommand(@"//sql query here", connection))
                {
                 command.Notification = null;
                 var notificationDependency = new SqlDependency(command);
                    notificationDependency.OnChange += new OnChangeEventHandler(dependency_OnChangeNotifications);

                 if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        ////reads the data
                    }
               }
          }
       }
 private void dependency_OnChangeNotifications(object sender, SqlNotificationEventArgs notification)
    {
        if (notification.Type == SqlNotificationType.Change)
        {
            MessagesHub.GetNotifications();
        }
    }

    private void dependency_OnChangeChatMessages(object sender, SqlNotificationEventArgs notification)
    {
        if (notification.Type == SqlNotificationType.Change)
        {
            MessagesHub.GetChatMessages();
        }
    }

如果两个人登录,则会创建两个sql依赖项。如果在Db中发生任何变化,事件将以2的倍数触发,第一次4接下来的8接下来的16接下来的32,依此类推。我想限制sql依赖的数量,因为我假设问题是因为这个。

Msdn说:

SqlDependency dependency=new SqlDependency(command);  
// Maintain the refence in a class member.  

我将如何维护引用,以便下次它不会创建新依赖项但使用现有依赖项。 或者,如果我的假设是错误的并且由于某些其他原因导致多个触发器,请告诉我。感谢

0 个答案:

没有答案