经过过去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 –