2017-04-06 18 views
2

この問題と過去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

+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(); 
} 

あなたはそれらの依存関係は、例えば(どのように機能するかを理解していることを確認してください - あなたは1つの通知を受け取った後 - あなたを後で取得するために再登録する必要があります)。または、より良い、いくつかのラッパーライブラリ、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

投稿する前に動作することを確認したので、他に問題がなければ動作するはずです。再登録の場合は、通知を1つ取得した後、もう一度このコマンドを実行するだけです(コマンドの作成、依存関係の作成、実行)。最初に単純なコンソールアプリケーションを作成し、シグナルがなくてもすべてがそこで動作することを確認します。 – Evk

+0

Evk、私も1つのコンソールアプリケーションを試しましたが、私はあまりにもうまくいきませんでした。データベースの変更はコード側で反映されません。イベントは起動されていません。私はこの素晴らしい記事を読んだ。 https://hendrikbulens.wordpress.com/2015/04/28/c-firing-code-after-database-changes-with-sqldependency-it/comment-page-1/#comment -355 –

関連する問題