2017-11-16 16 views
0

初めてSQL依存関係を使用しています...しかし、いくつかの例を上った後、私はすべて正しいことをしているように感じます。私はブローカーが有効であることを確認しました。私はさらに私のクエリが正しいことを確認しました。私は全く例外を受けていません!すべてとすべてがすべて動作するはずですが、そうではありません。例外を投げずにトラブルシューティングを始める方法がわかりません。SQL依存関係イベントがトリガーされていません

ご迷惑をおかけして申し訳ございません。

public class NotificationEvent 
{ 
    private delegate void RateChangeNotification(DataTable table); 
    private SqlDependency dependency; 
    string ConnectionString = @"ConnectionString"; 
    string UserName = Environment.UserName; 

    public async void StartNotification() 
    { 
     SqlDependency.Start(this.ConnectionString, "UserNotificationsQueue"); 
     SqlConnection connection = new SqlConnection(this.ConnectionString); 
     await connection.OpenAsync(); 

     SqlCommand command = new SqlCommand();   
     command.Connection = connection; 
     command.CommandType = CommandType.Text; 
     command.CommandText = string.Format("SELECT [NotificationID],[UserFrom],[UserTo],[DateTimeSent],[Notification] FROM [dbo].[PersonnellNotifications]", UserName); 
     command.Notification = null; 


     this.dependency = new SqlDependency(command, "Service=PostUserNotificationsQueue;", Int32.MaxValue); 
     dependency.OnChange += new OnChangeEventHandler(this.SqlDependencyOnChange); 
     await command.ExecuteReaderAsync(); 
    } 

    private void SqlDependencyOnChange(object sender, SqlNotificationEventArgs eventArgs) 
    { 
     if (eventArgs.Info == SqlNotificationInfo.Invalid) 
     { 
      Console.WriteLine("The above notification query is not valid."); 
     } 
     else 
     { 
      Console.WriteLine("Notification Info: " + eventArgs.Info); 
      Console.WriteLine("Notification source: " + eventArgs.Source); 
      Console.WriteLine("Notification type: " + eventArgs.Type); 
     } 
    } 


    public void StopNotification() 
    { 
     SqlDependency.Stop(this.ConnectionString, "QueueName"); 
    } 
} 

私が見られるように、別のクラスIniatializeComponent()からこれを初期化しています:私はちょうど私のコードとその作業良いで次のテストしている

private void InitializeComponent() 
{ 
    // Initialize SQL Dependancy   
    ne.StartNotification(); 
} 
+0

SQLの依存関係を持つ私の知識がかなり限られているが、あなたは、あなたが実際にサーバ上でやっているものを見るためにSQLプロファイラを接続していた場合、私は思っていました。 – Amy

+0

この初期設定は、Global.asax.Application_StartまたはStartup.csで行う必要があります。 – Programmer

+0

@Programmerあなたは正しいです...私は実際に別の問題に続いて数分前にそれを切り替えました。しかし、スタートアップレベルでも同じ結果が得られました。 –

答えて

1

はここに私のクラスです。私はあなたのコードを単純化しました。これが有効で、あなたがDb ChangeでOnNotificationChangeを呼び出しているかどうか確認してください。

public async void RegisterForNotification() 
{ 
    var connectionString = @"ConnectionString"; 
    using (var connection = new SqlConnection(connectionString)) 
    { 
     await connection.OpenAsync(); 

      var queryString = "Your Query String"; 
      using (var oCommand = new SqlCommand(queryString, connection)) 
      { 
       // Starting the listener infrastructure... 
       SqlDependency.Start(connectionString); 

       var oDependency = new SqlDependency(oCommand); 
       oDependency.OnChange += OnNotificationChange; 

       // NOTE: You have to execute the command, or the notification will never fire. 
       await oCommand.ExecuteReaderAsync(); 
      } 
     } 
    } 


private void OnNotificationChange(object sender, SqlNotificationEventArgs e) 
{ 
    Console.WriteLine("Notification Info: " + e.Info); 
    //Re-register the SqlDependency. 
    RegisterForNotification(); 
} 
+0

OPのコードではなく、あなたのコードが動作すると思う理由を説明した方が良い答えです。 –

+0

甘い!私は何時間もこの車輪を回転させてきた。私が間違って何をしていたのか? –

+0

また、イベントが発生した後に再登録する必要があることに気づいていませんでしたか?それは必要ですか? –

1

SQLClientPermissionを設定していますか?参照: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/enabling-query-notifications

// Code requires directives to 
// System.Security.Permissions and 
// System.Data.SqlClient 

private bool CanRequestNotifications() 
{ 
    SqlClientPermission permission = 
     new SqlClientPermission(
     PermissionState.Unrestricted); 
    try 
    { 
     permission.Demand(); 
     return true; 
    } 
    catch (System.Exception) 
    { 
     return false; 
    } 
} 
+0

あなたは正しいです...私はこれを見逃していました。残念ながらそれは私の問題を解決しませんでした。 –

関連する問題