SignalR重复消息在sqlDependency上更改

Jam*_*ars 5 c# sqldependency signalr angularjs

我开发了一个连接到signalR hub的网页,它连接到带有angularjs的jquery代码.sqldependency.onchange发生事件时会向客户端发送消息,但是,对于连接的每个客户端,消息都会复制到每个客户端.因此,如果连接了两个客户端,则每个客户端都会收到两条消息,依此类推.

以下是步骤:

  • 将两个客户端连接到集线器
  • 进行DB更改
  • 触发sqlDependency.onchange
  • 呼叫集线器功能 clients.all.renewProducts()
  • 重新创建数据存储库重置依赖项
  • 客户控制台: "Database SQL Dependency change detected: Update" app.js:44:12 (Twice)

Hub.cs

public static void SignalRGetData(string data)
{
      IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SignalRGetData>();
      context.Clients.All.renewData(data);

      // Recereate data and sql dependency
      new DataRespository().GetData();
}
Run Code Online (Sandbox Code Playgroud)

DataRespository.cs _dependency_OnChange

public void _dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
    if(e.Info == SqlNotificationInfo.Update)
    {
        ProductHub.GetProducts("Database SQL Dependency change detected: " + e.Info);
    }
Run Code Online (Sandbox Code Playgroud)

}

的GetData

public IEnumerable<ProductInventoryDetail> GetData()
{
     using(var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DynamicPricing"].ConnectionString))
     {
          conn.Open();
          var reposQuery =
          "SELECT [ID], [Program] FROM [DBO].[Detail]";
          using(SqlCommand cmd =  new SqlCommand(reposQuery, conn))
          {
                // remove any command object notifications
                cmd.Notification = null;
                // create dependency
                SqlDependency dependency = new SqlDependency(cmd);
                dependency.OnChange += new OnChangeEventHandler(_dependency_OnChange);

                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();

                // Execute Sql Command
                using(var reader = cmd.ExecuteReader())
                {
                    return reader.Cast<IDataRecord>().Select(x => new ProductInventoryDetail(){

                         ID = x.GetInt32(0),
                         Program = x.GetInt32(1)
                     }
                 }
           }
      }
}
Run Code Online (Sandbox Code Playgroud)

角度JavaScript

// Apply jQuery SignalR operations to Angular
app.value('$', $);

app.factory('signalRService', ['$', '$rootScope', function ($, $rootScope) {

    var proxy = null;
    var initialise = function () {
        // Get Connection to SignalR Hub
        var connection = $.hubConnection();

        // Create a Proxy
        proxy = connection.createHubProxy('SignalRData');

        // Publish the event when server has a push notification
        proxy.on('renewProducts', function (message) {
            console.log("Database SQL Dependency change detectedgnalRGetData: " + message);
            $rootScope.$emit('renewProducts', message);
        });

        // Start Connection
        connection.start().done(function () {
            console.log("Conenction Ready - invoke proxy");
            proxy.invoke('SignalRGetData');
        });

    };

    return {
        initialise: initialise
    }
}]);
Run Code Online (Sandbox Code Playgroud)