简体   繁体   中英

Multiple Calls from SignalR client

on my asp.net mvc application i'm using signalR to notify db changes to application. my problem is once i page reloaded. signalR client get called multiple time.

i used SqlDependency for get sql db changes my code as below

public List<Load> GetAllReportSignalR()
{
    var loads = new List<Load>();
    var connectionString = _context.Database.Connection.ConnectionString;
    using (var connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(@"SELECT LoadId, [FileName], LoadTypeId, CreatedDate, CreatedBy, LoadStatusId FROM [dbo].[Load]", connection))
        {
            command.Notification = null;
            var dependency = new SqlDependency(command);
            dependency.OnChange +=new OnChangeEventHandler(dependency_OnChange);
            if (connection.State == ConnectionState.Closed)
                connection.Open();
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                loads.Add(
                        new Load
                        {
                            FileName = (string)reader["FileName"],
                            CreatedBy = (string)reader["CreatedBy"],
                            CreatedDate = (DateTime)reader["CreatedDate"],
                            LoadId = (int)reader["LoadId"],
                            LoadStatusId = (int)reader["LoadStatusId"],
                            LoadTypeId = (short)reader["LoadTypeId"]
                        });
            }
        }

    }
    return loads;
}


private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{

    if (e.Type == SqlNotificationType.Change)
    {
        Notifier.UpdateDataTable();
    }

}

On my hub

[HubMethodName("updateDataTable")]
public void UpdateDataTable()
{
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<Notifier>();
    context.Clients.All.updateData();
}

then i call signalR client

   var notifier = $.connection.notifier;
notifier.client.updateData = function () {
    $.ajax({
        type: 'POST',
        url: '/Home/LoadData',
        success: function (d) {
            reloadTbl(d)
        }
    });
};

$.connection.hub.start().done(function () {
    $.ajax({
        type: 'POST',
        url: '/Home/LoadData',
        success: function (d) {
            reloadTbl(d)
        }
    });
}).fail(function(e) {

});

I started and stopped SqlDependency on Application_Start() and Application_End()

i did follow below example and that has the same problem ( CodeProject Tutorial )

When your page is loaded you did

$.connection.hub.start().done(function () {
    $.ajax({
        type: 'POST',
        url: '/Home/LoadData',
        success: function (d) {
            reloadTbl(d)
        }
    });
})

which called back to the server to load data.

In addition, you had

var dependency = new SqlDependency(command);
dependency.OnChange +=new OnChangeEventHandler(dependency_OnChange);
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{

    if (e.Type == SqlNotificationType.Change)
    {
        Notifier.UpdateDataTable();
    }

}

which registered to data changed. For the callback above, it would trigger dependency_OnChange , which then called back to the client side.

Therefore, your page was refreshed twice. One was from server pushed, and the other from client pulled.

Solution: You may choose to push from server to client when the page is loaded and remove the client pulls code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM