SignalR 两次访问数据库
SignalR is hitting database twice
数据库 table(DummyData) 有一个列 (Message),只有一个 record/row 的值为 "hello"。我正在使用 signalR 在网页上显示这个值.每当在数据库中更新此值时,网页上的文本也会在不刷新的情况下更新。这一切都很好。
我看到的问题是,应用程序两次访问数据库。这是设计使然还是错误的代码。 (该页面仅打开一次。没有其他实例)
aspx
<script>
$(function () {
var notify = $.connection.notificationsHub;
$.connection.hub.start().done(function () {
notify.server.notifyAllClients();
});
notify.client.displayNotification = function (msg) {
$("#newData").html(msg);
};
});
</script>
<span id="newData"></span>
aspx.cs
public string SendNotifications()
{
using (SqlConnection connection = new SqlConnection(conStr))
{
string query = "SELECT [Message] FROM [dbo].[DummyData]";
SqlCommand command = new SqlCommand(query, connection)
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
message = reader[0].ToString();
}
}
return message;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationsHub obj = new NotificationsHub();
obj.NotifyAllClients();
}
}
NotificationsHub.cs
public class NotificationsHub : Hub
{
Messages obj = new Messages();
public void NotifyAllClients()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
context.Clients.All.displayNotification(obj.SendNotifications());
}
public override Task OnConnected()
{
NotifyAllClients();
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
NotifyAllClients();
return base.OnDisconnected(stopCalled);
}
}
以下是调试时如何命中断点:
页面加载时:
- OnConnected()
- NotifyAllClients()
- 发送通知()
- NotifyAllClients()
//why is this hit again
- 发送通知()
当我运行update DummyData Set Message='helloworld'
- dependency_OnChange()
- NotifyAllClients()
- 发送通知()
- dependency_OnChange()
//hit a second time here too
- NotifyAllClients()
- 发送通知()
至少对于初始页面加载,我是这样假设的:
您在 OnConnected
中的客户端连接上调用 NotifyAllClients
,然后在客户端的 done
函数中又调用了 NotifyAllClients
。
数据库 table(DummyData) 有一个列 (Message),只有一个 record/row 的值为 "hello"。我正在使用 signalR 在网页上显示这个值.每当在数据库中更新此值时,网页上的文本也会在不刷新的情况下更新。这一切都很好。
我看到的问题是,应用程序两次访问数据库。这是设计使然还是错误的代码。 (该页面仅打开一次。没有其他实例)
aspx
<script>
$(function () {
var notify = $.connection.notificationsHub;
$.connection.hub.start().done(function () {
notify.server.notifyAllClients();
});
notify.client.displayNotification = function (msg) {
$("#newData").html(msg);
};
});
</script>
<span id="newData"></span>
aspx.cs
public string SendNotifications()
{
using (SqlConnection connection = new SqlConnection(conStr))
{
string query = "SELECT [Message] FROM [dbo].[DummyData]";
SqlCommand command = new SqlCommand(query, connection)
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
message = reader[0].ToString();
}
}
return message;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationsHub obj = new NotificationsHub();
obj.NotifyAllClients();
}
}
NotificationsHub.cs
public class NotificationsHub : Hub
{
Messages obj = new Messages();
public void NotifyAllClients()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
context.Clients.All.displayNotification(obj.SendNotifications());
}
public override Task OnConnected()
{
NotifyAllClients();
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
NotifyAllClients();
return base.OnDisconnected(stopCalled);
}
}
以下是调试时如何命中断点:
页面加载时:
- OnConnected()
- NotifyAllClients()
- 发送通知()
- NotifyAllClients()
//why is this hit again
- 发送通知()
当我运行
update DummyData Set Message='helloworld'
- dependency_OnChange()
- NotifyAllClients()
- 发送通知()
- dependency_OnChange()
//hit a second time here too
- NotifyAllClients()
- 发送通知()
至少对于初始页面加载,我是这样假设的:
您在 OnConnected
中的客户端连接上调用 NotifyAllClients
,然后在客户端的 done
函数中又调用了 NotifyAllClients
。