SignalR两次击中数据库

时间:2015-07-28 10:41:26

标签: c# asp.net signalr

数据库表(DummyData)只有一个列(Message),只有一个记录/行的值为" hello" .I' m使用signalR在网页上显示该值。每当在DB中更新此值时,网页上的文本也会在不刷新的情况下更新。这一切都很好。

我看到的问题是,应用程序正在两次访问数据库。这是设计还是坏代码。 (页面只打开一次。没有其他实例)

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);
   }
 }

以下是调试时断点的方法:

    页面加载时
    1. OnConnected()
    2. NotifyAllClients()
    3. SendNotifications()
    4. NotifyAllClients()//why is this hit again
    5. SendNotifications()
  • 当我运行update DummyData Set Message='helloworld'

    1. dependency_OnChange()
    2. NotifyAllClients()
    3. SendNotifications()
    4. dependency_OnChange()//hit a second time here too
    5. NotifyAllClients()
    6. SendNotifications()

1 个答案:

答案 0 :(得分:3)

至少对于初始页面加载我假设:

您在public class MainViewModel : ViewModelBase { private readonly ManagerDbContext _context = new ManagerDbContext(); public MainViewModel() { } private IEnumerable<Departments> _departments; public ObservableCollection<Departments> Departments { get { return new ObservableCollection<Departments>(_context.Departments); } set { _departments = value; RaisePropertyChanged("Departments"); } } private IEnumerable<Employee> _employee; public IEnumerable<Employee> Employees { get { return new ObservableCollection<Employee>(_context.Employees.Include(e => e.Department)); } set { _employee = value; RaisePropertyChanged("Employees"); } } private Employee _currentSelectedEmployee; public Employee CurrentSelectedEmployee { get { return _currentSelectedEmployee; } set { _currentSelectedEmployee = value; RaisePropertyChanged("CurrentSelectedEmployee"); } } } 中的客户端连接上呼叫NotifyAllClients,然后在客户端的OnConnected功能中再次呼叫done