[英]Multiple Calls from SignalR client
on my asp.net mvc application i'm using signalR to notify db changes to application. 在我的asp.net mvc应用程序上,我正在使用signalR通知数据库对应用程序的更改。 my problem is once i page reloaded. 我的问题是,一旦我重新加载页面。 signalR client get called multiple time. signalR客户端被多次调用。
i used SqlDependency
for get sql db changes my code as below 我使用SqlDependency
获取SQL数据库更改我的代码如下
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 然后我打电话给SignalR客户
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()
我在Application_Start()
和Application_End()
上启动和停止了SqlDependency
i did follow below example and that has the same problem ( CodeProject Tutorial ) 我确实按照以下示例进行操作,并且存在相同的问题( CodeProject教程 )
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. 对于上面的回调,它将触发dependency_OnChange ,然后将其回调回客户端。
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. 解决方案:您可以选择在页面加载时从服务器推送到客户端,并删除客户端请求代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.