locked
SignalR performing the same call multiple times. RRS feed

  • Question

  • User228832095 posted

    hi,

    I'm having issues getting signalr to simply just update my notifications once, when a change is made. Currently if I make a database call that changes a table signalR initiates the same call an extra 3 or 4 times and the more times I refresh the page the more times the calls are made when a data change occurs. I'm not sure if it is initiating additional on change events or what it is doing. Im using signalR 1 with MVC 5. Please see the code I have so far below: 

          readonly string _connString =
            ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            private SqlDependency dependency;
            public async Task<List<NotificationsModel>> GetAllMessages(Int64 userId)
            {
                var messages = new List<NotificationsModel>();
                using (var connection = new SqlConnection(_connString))
                {
                    connection.Open();
                    using (var command = new SqlCommand(@"SELECT ([NotificationID_PK]) as 'NotificationID' FROM [dbo].[Notifications] where [DateTime] >=" + DateTime.Now.ToShortDateString(), connection))
                    {
                        command.Notification = null;
                        dependency = new SqlDependency(command);
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                        if (connection.State == ConnectionState.Closed)
                            connection.Open();
    
                        var reader = await command.ExecuteReaderAsync();
    
                    }
    
                }
                return messages;
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {
                if (e.Type == SqlNotificationType.Change)
                {
                    MessagesHub.SendMessages();
                }
                dependency = sender as SqlDependency;
                dependency.OnChange -= new OnChangeEventHandler(dependency_OnChange);
            }

    Javascript Function to initiate connection:

    $(function () {
    // Declare a proxy to reference the hub.
    var notifications = $.connection.messagesHub;

    // Create a function that the hub can call to broadcast messages.
    notifications.client.updateMessages = function () {
    getAllMessages()

    };
    // Start the connection.
    $.connection.hub.start().done(function () {
    console.log("Connection");
    getAllMessages();
    }).fail(function (e) {
    alert(e);
    });

    });

    Monday, July 30, 2018 12:24 PM

All replies

  • User61956409 posted

    Hi Frijx,

    Please share the implementation of you hub method SendMessages and javascript function getAllMessages().

    Besides, please set breakpoint in SqlDependency OnChange method and check the execution of your code.

    With Regards,
    Fei Han

    Tuesday, July 31, 2018 5:32 AM