SQL Dependency & SignalR - 两个具有相同代码的 Web 应用程序,一个在工作而另一个不工作

SQL Dependency & SignalR - Two web apps with the same code, one is working while the other is not

简介:

There's a TL;DR at the end (:

我有两个版本的 Web 应用程序,它们使用 SQL Dependency 和 SignalR 在检测到数据库发生变化时自动刷新视图。

以前版本的应用程序可以正常工作,而新版本则不能。 奇怪的是,乍一看,两个版本都有相同的代码,两个网络应用程序之间的 SignalR 部分没有变化。


NotifierEntity.cs:

当您启动应用程序时,两个版本都在后端正确执行以下代码:

void RegisterForNotifications()
{
    using (var sqlConnection = new SqlConnection(notificationEntity.SqlConnectionString))
    using (var sqlCommand = new SqlCommand(notificationEntity.SqlQuery, sqlConnection))
    {
        foreach (var sqlParameter in notificationEntity.SqlParameters)
            sqlCommand.Parameters.Add(sqlParameter);

        sqlCommand.Notification = null;
        var sqlDependency = new SqlDependency(sqlCommand);
        sqlDependency.OnChange += OnSqlDependencyChange;
        if (sqlConnection.State == ConnectionState.Closed)
            sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlCommand.Parameters.Clear();
    }
}

注意:新版本中的OnChange事件永远不会触发。


我要刷新的视图:

我要自动刷新的视图包含如下脚本,其作用是订阅DB中改变一个table的事件
与上面的代码一样,此代码在两个版本的网络应用程序之间是相同的。

<script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/anotherScript.min.js"></script>
<script type="text/javascript">
    var signalRHubInitialized2 = false;

    $(function () {
        InitializeSignalRHubStore2();
    });
    function InitializeSignalRHubStore2() {
        if (signalRHubInitialized2)
            return;
        try {
            var clientHub2 = $.connection.hubNotificacion;
            clientHub2.client.updateTableVehicles = function (message) {
                if (message === "Refresh")
                    ReloadIndexPartial2();
            };
            $.connection.hub.start().done(function () {
                clientHub2.server.initializeTableDependencyVehicles();
                signalRHubInitialized2 = true;
            });
        } catch (err) {
            signalRHubInitialized2 = false;
        }
    };
    function ReloadIndexPartial2() {
        location.reload();
    }
</script>

枢纽:

当您加载视图和 运行 该脚本时,将在后端访问以下中心代码。代码的第一部分似乎在两个版本的 Web 应用程序中都能正常工作:

public void InitializeTableDependencyVehicles()
{
    if (!flag)
    {
        using (var db = new ApplicationDbContext())
        {
            string data = db.GetNotifierEntity<Vehicle>(db.Vehicles).ToJson();
            NotifierEntity NotifierEntity1 = NotifierEntity.FromJson(data);
            Action<String> dispatcher = (t) => { DispatchToClientTablesVehicles(); };
            PushSqlDependency.Instance(NotifierEntity1, dispatcher);
            flag = true;
        }
    }
}

但是第二个代码自动 运行s 仅在 Web 应用程序的一个版本 上。当数据库中的记录更新时,另一个版本似乎不会触发刷新事件。请记住,在肉眼看来,两个网站在 SignalR 和 SQL 依赖项部分具有相同的代码。

public void DispatchToClientTablesVehicles()
{
    Clients.All.updateTableVehicles("Refresh");
}

长话短说

同一网络应用程序的两个版本具有相同的代码,但是网络应用程序的一个版本检测到数据库中的更改并触发一个事件,而另一个版本则没有。

  • Both Web Applications run the same code (the SignalR and SQL Dependency stuff)

  • One Web Application does refresh automatically the view, but the other does not.

谁能帮我解决这个问题?

我设法通过删除数据库并重新创建它来解决这个问题。 我已经生成了脚本来再次填充数据并且它再次起作用。

重要提示:从 .bak 备份文件恢复数据库无效。我不得不从头开始创建一个新数据库。

P.S。 (这样您就可以避免以后头疼):我注意到 SignalR 在生成一些并发连接时会很快停止工作。如果您打算同时登录 10 个用户,这不是一个好主意。