【发布时间】: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