[英]SqlDependency.OnChange dependency_OnChange did not fire
我刚刚根据SO的不同文章和多个问题开始使用SignalR
和设置配置。 我遵循了每一个步骤。 我无法弄清楚为什么不触发依赖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) {
});
});
启动文件
[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);
}
我已经弄清楚了,并将其发布为答案,以便将来将要遇到此类问题的任何读者都可以解决。
我调试的代码,发现我得到以下参数与价值观SqlNotificationEventArgs
在dependency_OnChange
和那些有:
Info => Invalid
Type => Subscribe
Source => Statement
如果信息无效,这将使我知道查询存在问题。 然后,我更改了查询语法,如下所示,它运行良好。
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
以下是我发现的查询状态:
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都会触发刷新页面的次数。 例如,如果页面刷新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();
}
}
服务控制器
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.