【发布时间】:2022-01-18 07:08:43
【问题描述】:
大家好,提前感谢您的帮助。我正在开发一个 SignalR .Net 5 MVC 应用程序,它使用 SignalR 通知客户端记录到数据库的入站呼叫。我正在使用 javascript sdk 来执行客户端通知。我还确认了 SQL Server Service Broker 已启用并且正在工作。这是我的控制器代码:
public class CallNotificationHub : Hub
{
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
}
public async Task SendCallNotifications()
{
try
{
string connectionString = myConnString;
SqlDependency.Start(connectionString);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT [ID], [TimeEntered], [CallFrom], [CallTo] FROM [dbo].[tbl_Log_InboundTwilioCalls]";
command.Connection = connection;
command.CommandType = CommandType.Text;
dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
DataTable dt = new DataTable();
dependency.AddCommandDependency(command);
var reader = command.ExecuteReader();
SendEmail sendEmail = new SendEmail("SendCallNotifications Reached", "New SignalR5 SendCallNotifications at " + DateTime.Now.ToString(), "");
}
catch (Exception ex)
{
SendEmail sendErrEmail = new SendEmail("SendCallNotifications Error", ex.ToString(), "");
}
await Clients.All.SendAsync("RecieveNotification", "Changed at " + DateTime.Now.ToString());
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
SendEmail sendEmail = new SendEmail("Dependency Change", e.Type.ToString() + " at " + DateTime.Now.ToString(), "");
CallNotificationHub nHub = new CallNotificationHub();
nHub.SendCallNotifications();
}
}
javascript 通知脚本:
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/CallNotificationHub").build();
connection.on("RecieveNotification", (result) => {
console.log(result);
alert("Result=" + result);
alert("CallNotification2 Changed");
});
connection.start().then(function () {
connection.invoke("SendCallNotifications").catch(err => console.error(err));
}).catch(function (err) {
return console.error(err.toString());
});
在初始页面加载时,通知按预期工作并将响应写入控制台。但是,当数据库更新时,会触发电子邮件通知,但是客户端不会更新。我坚持从这里去哪里。任何帮助将不胜感激。
【问题讨论】:
-
请检查输出中的错误信息。
-
我看到您在 2021 年 5 月 25 日创建了相同的问题,以及创建示例帖子的原因。问题仍然存在或修改后问题重现。请关注项目近期是否更新部署。
-
输出中没有我可以看到的错误消息。我永远无法让这个项目与 .Net Core 一起使用。相反,我恢复到 .Net 4.5。
-
请启用跟踪功能和use debugdiag tool to collect the logs。您需要先在 IIS 中托管您的应用程序,然后尝试重现问题,您可以获得日志。
-
我会尝试,但我真的不认为这是一个 IIS 问题。在同一个项目中,我有一个可用的 SignalR 聊天应用程序。