2017-04-06 124 views
2

经过过去3天的这个问题挣扎后,最后我把我的问题放在这里。为什么SqlDependency.OnChange没有被解雇?

我想使用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

+0

https://msdn.microsoft.com/en-us/library/ms181122.aspx –

回答

2

您还没有执行你的命令,那就是需要得到通知:

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之一。

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

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

感谢EVK。我刚刚做到了。不幸的是,没有成功。 TBH,我也是这么做的。但它没有工作。不过,我只是想知道这是什么意思,“你需要重新注册才能获得后续”。我如何在当前代码中执行此操作? –

+0

我证实它在发布前有效,所以它应该工作,除非有其他问题。至于重新注册 - 您只需要在获得一次通知后再次执行所有操作(创建命令,创建依赖项,执行)。首先创建简单的控制台应用程序,并确保所有在那里工作,没有signalr。 – Evk

+0

Evk,我已经尝试了一个控制台应用程序,但我也没有工作。数据库更改没有反映在代码方面。事件没有被解雇。我读过这篇不错的文章 https://hendrikbulens.wordpress.com/2015/04/28/c-firing-code-after-database-changes-with-sqldependency-it/comment-page-1/#comment -355 –