2017-04-06 98 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 –