【问题标题】:Why SqlDependency.OnChange is not being fired?为什么 SqlDependency.OnChange 没有被解雇?
【发布时间】:2017-09-01 12:01:55
【问题描述】:

在过去 3 天一直在努力解决这个问题之后,我终于把我的问题放在这里了。

我正在尝试使用 SignalR 和 SqlDependency 构建实时应用程序。显然,SQLDependency 不起作用。但是,我的 SignalR 运行良好,因为我尝试了许多不需要数据库交互的功能。

下面是我的代码。 Here 是我引用的。

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    NotificationHub objNotificationHub;

    protected void Application_Start()
    {
        string connectionString = WebConfigurationManager.AppSettings["SQL"];
        // SQL Command Text
        string commandText = "SELECT status From tableTask";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(commandText, connection);
            SqlDependency dependency = new SqlDependency(command);
            dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);    
            SqlDependency.Start(connectionString);
            command.ExecuteReader().Dispose();
            objNotificationHub = new NotificationHub();
        }
    }

    private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change)
        {
            objNotificationHub.SendNotifications();
        }
    }
}

NotificationHub.cs(SignalR 集线器类)

[HubMethodName("sendNotifications")]
public void SendNotifications()
{
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
    context.Clients.All.recieveNotification("asbc");
}

我的 SqlDependency.OnChange 事件,即 dependency_OnChange 没有触发 关于数据库更新。

我已经尝试了所有的方法,比如授予权限

ALTER DATABASE MyDB SET ENABLE_BROKER

还有很多类似的。 但没有成功。

会不会有我遗漏的东西。另外,有什么方法可以检查我的代码是否与 SQL Server 通信?

TIA

【问题讨论】:

标签: c# .net sql-server signalr sqldependency


【解决方案1】:

您没有执行您的命令,这是获取通知所必需的:

SqlDependency.Start(connectionString);
string commandText = "SELECT status From dbo.tableTask"; // don't forget schema here
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    SqlDependency dependency = new SqlDependency(command);
    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);    
    command.ExecuteReader().Dispose();
    objNotificationHub = new NotificationHub();
}

确保您了解这些依赖项是如何工作的(例如 - 在您收到一个通知后 - 您需要重新注册才能获得后续通知)。或者更好的是,使用一些包装库,例如 this one。

你可以用这个简单的例子来测试它:

static void Main(string[] args) {
    var cs = "connection string";        
    using (SqlConnection connection = new SqlConnection(cs))
    {
        connection.Open();
        SqlCommand command = new SqlCommand("select ErrorCode from dbo.Error", connection);
        SqlDependency dependency = new SqlDependency(command);
        dependency.OnChange += OnChange;
        SqlDependency.Start(cs);
        command.ExecuteReader().Dispose();                
    }
    Console.ReadKey();
}

private static void OnChange(object sender, SqlNotificationEventArgs e) {
    Console.WriteLine(e.Info);
}

【讨论】:

  • 感谢 Evk。我就是这么做的。不幸的是,没有成功。 TBH,我之前也这样做过。但它不起作用。不过,我只想知道“您需要重新注册才能获得后续”是什么意思。如何在当前代码中执行此操作?
  • 我在发布之前验证了它可以工作,所以除非有其他问题,否则它应该可以工作。至于重新注册 - 您只需要在收到一个通知后再次执行所有这些(创建命令,创建依赖项,执行)。首先创建简单的控制台应用程序并确保所有工作都在那里,没有信号器。
  • Evk,我也尝试了一个控制台应用程序,但我也没有工作。数据库更改不会反映在代码方面。事件没有被触发。我读过这篇不错的文章hendrikbulens.wordpress.com/2015/04/28/…
  • 然后发布该控制台应用程序的代码(最少需要重现),我们将找出问题所在。
  • @MukeshKumar 我看到您使用“SELECT status From tableTask”作为命令文本。你如何触发新的通知,用什么语句?插入\更新,哪些列?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多