2017-04-05 15 views
0

私はちょうどSignalRを使用し始めました。私はあらゆるステップに従ってきました。依存関係OnChangeがなぜ発火していないのか分かりません。SqlDependency.OnChange dependency_OnChangeが起動しなかった

[HubName("broadcastHub")] 
public class BroadcastHub : Hub 
{ 
    [HubMethodName("sendNotifications")] 
    public Task<object> SendNotifications() 
    { 
     DataTable dt = new DataTable(); 
     using (var connection = new SqlConnection(strConnectionString)) 
     { 
      connection.Open(); 
      using (SqlCommand command = new SqlCommand(query, connection)) 
      { 
       command.Notification = null; 
       SqlDependency dependency = new SqlDependency(command); 
       dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); 
       var reader = command.ExecuteReader(); 
       dt.Load(reader); 
       connection.Close(); 

      } 
     } 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>(); 
     var json = Newtonsoft.Json.JsonConvert.SerializeObject(dt); 
     return (context.Clients.All.RecieveNotification(json)); 
    } 
    private void dependency_OnChange(object sender, SqlNotificationEventArgs e) 
    { 
     if (e.Type == SqlNotificationType.Change) 
     { 
      SendNotifications(); 
     } 
    } 
} 

初めて正常に動作し、予想されるデータが得られます。しかし、どのような変更が表に行われたとき、それはdependency_OnChange

を発射しない、私はまた、ブローカサービスは、次のクエリを使用して有効になっていることを確認した: -

select is_broker_enabled from sys.databases where name='msdb' 
select is_broker_enabled from sys.databases where name='mydb' 

の両方がenabledであり、値が1です。私はSendNotificationsに使用しています

クエリは次のとおりです。 -

select Id,OXEName,OXEIP IP,ConnectionStatus Status, Case WHEN ConnectedOxeIP IS NULL OR ConnectedOxeIP = '' THEN OXEIP ELSE ConnectedOxeIP END as ConnectedOxeIP from PBXDetail

Javaスクリプト

$(function() { 
    var notifications = $.connection.broadcastHub; 
    notifications.client.recieveNotification = function (response) { 
    }; 
    $.connection.hub.start().done(function() { 
     notifications.server.sendNotifications(); 
    }).fail(function (e) { 
    });    
}); 

Startup.cs

[assembly: OwinStartup(typeof(myNameSpace.Startup))] 
namespace myNameSpace 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(new HubConfiguration() { EnableJSONP = true }); 
     } 
    } 
} 

Global.asaxの

protected void Application_Start(object sender, EventArgs e) 
{ 
    System.Data.SqlClient.SqlDependency.Start(strConnectionString); 
} 

protected void Application_End(object sender, EventArgs e) 
{ 
    System.Data.SqlClient.SqlDependency.Stop(strConnectionString); 
} 

答えて

1

私はそれを考え出したとこの種の問題に直面することになる任意の将来の読者がそれを把握することが可能であろうように、答えとして、それを投稿しています。

私は、コードの一部をデバッグし、私はdependency_OnChangeSqlNotificationEventArgsの値と、次のパラメータを取得して、それらがあることがわかった:情報が無効である場合

Info => Invalid 
Type => Subscribe 
Source => Statement 

すると、これは私に問題があったことを知っているように私につながりますクエリ。次に、次のようなクエリの構文を変更して正常に動作しました。

select * from table // did not work 
select ID from table // did not work 
select [ID] from table // did not work 
select [ID] from dbo.table // Worked 

これを行った後、私はすべてのページのリフレッシュでdependency_OnChangeページがリフレッシュしたように何回も発射されたことを発見した:

select [Id],[OXEName],[OXEIP] as [IP],[ConnectionStatus] as [Status], Case WHEN [ConnectedOxeIP] IS NULL OR [ConnectedOxeIP] = '' THEN [OXEIP] ELSE [ConnectedOxeIP] END as [ConnectedOxeIP] from dbo.PBXDetail 

は、私が見つけたクエリの状態です。例えば、ページが10回リフレッシュされた場合、それは10回発射されます。だから私は、以下の変更を行いました。

[HubMethodName("sendNotifications")] 
public Task<object> SendNotifications() 
{ 
    DataTable dt = new DataTable(); 
    using (var connection = new SqlConnection(strConnectionString)) 
    { 
     connection.Open(); 
     using (SqlCommand command = new SqlCommand(query, connection)) 
     { 
      command.Notification = null; 
      if (ServiceController.dependency == null) 
      { 
       ServiceController.dependency = new SqlDependency(command); 
       ServiceController.dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); 
      } 
      var reader = command.ExecuteReader(); 
      dt.Load(reader); 
      connection.Close(); 
     } 
    } 
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>(); 
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(dt); 
    return (context.Clients.All.RecieveNotification(json)); 
} 

private void dependency_OnChange(object sender, SqlNotificationEventArgs e) 
{ 
    if (e.Type == SqlNotificationType.Change) 
    { 
     if (ServiceController.dependency != null) 
     { 
      ServiceController.dependency.OnChange -= dependency_OnChange; 
      ServiceController.dependency = null; 
     } 
     SendNotifications(); 
    } 
} 

ServiceControllerの

public static class ServiceController 
{ 
    internal static SqlCommand command = null; 
    internal static SqlDependency dependency = null; 
    internal static bool isCachingEnabled = false; 
} 

Global.asaxの

protected void Application_Start(object sender, EventArgs e) 
{ 
    if (!ServiceController.isCachingEnabled) 
    { 
     SqlDependency.Stop(strConnectionString); 
     SqlDependency.Start(strConnectionString); 
    } 
} 
関連する問題