Sql server SqlNotificationEvents与Signal导致多次激发?
我是在asp.net mvc项目中使用signalr捕捉用户通知的新手。我跟着 这是我的ajax调用Sql server SqlNotificationEvents与Signal导致多次激发?,sql-server,ajax,asp.net-mvc,signalr,sqldependency,Sql Server,Ajax,Asp.net Mvc,Signalr,Sqldependency,我是在asp.net mvc项目中使用signalr捕捉用户通知的新手。我跟着 这是我的ajax调用 $(function () { // Proxy created on the fly var job = $.connection.ntfHub; // Declare a function on the job hub so the server can invoke it job.client.displayStatus
$(function () {
// Proxy created on the fly
var job = $.connection.ntfHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
toastr.warning("1 Bildirim Alındı!");
getData();
};
// Start the connection
$.connection.hub.start().done(function () {
console.log("connection started")
getData();
}).fail(function (e) {
alert(e);
});
});
function getData() {
var tbl = $("#header_notification_bar")
$.ajax({
url: '@Url.Action("GetNtf","Home")',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
console.log("called");
}).error(function () {
});
}
这是我的SQL查询
SELECT
[nu].[NtU_ID], [n].[Ntf_Title], [n].[Ntf_Description],
[u].[Usr_NameSurname], [n].[Ntf_Date]
FROM
[dbo].[SysNotificationUser] nu
INNER JOIN
[dbo].[SysUser] u ON [nu].[NtU_UserID] = [u].[ID]
INNER JOIN
[dbo].[SysNotifications] n ON [nu].[NtU_NtfID] = [n].[Ntf_ID]
WHERE
[nu].[NtU_UserID] = 2
AND [nu].[NtU_IsRead] = 0
关于使用类似信号器的启动类的其他问题与本教程中的相同
第一次发生这种变化时会出现一些奇怪的情况,ajax调用了1次,但是ajax调用的其他变化越来越大。除了第一次,它不会为一个改变打一个电话。例如,在console中,第一次只调用了一个,但之后调用了8次,调用了16次,依此类推
存储库中
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NtfHub.Show();
}
}
和集线器类
public void Hello()
{
Clients.All.hello();
}
[HubMethodName("show")]
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NtfHub>();
context.Clients.All.displayStatus();
}
执行命令的方法
public List<PopulateNtfBar> GetData()
{
DataTable dt = new DataTable();
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [NtU_ID] FROM [dbo].[SysNotificationUser] WHERE [NtU_UserID] =2 AND [NtU_IsRead] = 0", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += dependency_OnChange;
if (connection.State == ConnectionState.Closed)
connection.Open();
command.ExecuteScalar();
}
using (SqlCommand command2 = new SqlCommand(@"SELECT [nu].[NtU_ID],[n].[Ntf_Title],[n].[Ntf_Description],[u].[Usr_NameSurname],[n].[Ntf_Date] FROM [dbo].[SysNotificationUser] nu INNER JOIN [dbo].[SysUser] u ON [nu].[NtU_UserID]=[u].[ID] INNER JOIN [dbo].[SysNotifications] n ON [nu].[NtU_NtfID]=[n].[Ntf_ID] WHERE [nu].[NtU_UserID] =2 AND [nu].[NtU_IsRead] = 0", connection))
{
using (var reader = command2.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(item => new PopulateNtfBar()
{
Description = item["Ntf_Description"].ToString(),
Title = item["Ntf_Title"].ToString(),
TimeDiff = FindDifferenceTime(Convert.ToDateTime(item["Ntf_Date"]))
}).ToList();
}
}
}
如何以及在何处触发job.client.displayStatus?@MarcusH我编辑了问题只是一个细节:tbl.empty.appendresult;=在服务器端方法Show中:您可以在displayStatushtml中将html作为参数发送,并跳过客户端对html的ajax调用。然后,您可以跳过对服务器的一次呼叫,您的站点将运行得更快。@MarcusH您可以详细解释您的答案吗?它是否删除了多个呼叫?如何以及在何处触发job.client.displayStatus?@MarcusH我编辑了问题只是一个细节:tbl.empty.appendresult;=在服务器端方法Show中:您可以在displayStatushtml中将html作为参数发送,并跳过客户端对html的ajax调用。然后,您可以跳过一个呼叫服务器,您的站点将运行得更快。@MarcusH您能详细解释一下您的答案吗?它会删除多个呼叫吗?