この問題と過去3日間で闘った結果、ここで問題を解決しています。なぜSqlDependency.OnChangeが起動されていないのですか?
私はSignalRとSqlDependencyを使ってリアルタイムアプリケーションを構築しようとしています。どうやら、SQLDependencyは動作していません。しかし、私のSignalRは正常に動作しています。私は、データベースとのやり取りを必要としない多くの機能を試してきました。
以下は私のコードです。 Hereは参考にしています。すなわちdependency_OnChangeがデータベース上 を発射されていない
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イベント更新。
私は権限
のALTER DATABASE mydbというSET ENABLE_BROKER
と、このような他の多くを許可するようにすべての方法を試してみました。 しかし成功はありません。
私には紛失しているものがありますか?また、私のコードがSQL Serverと通信しているかどうかを確認する方法はありますか?
あなたのコマンドを実行していない、そしてそれは、通知を得るために必要とされるTIA
https://msdn.microsoft.com/en-us/library/ms181122.aspx –