經過過去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
https://msdn.microsoft.com/en-us/library/ms181122.aspx –