Hello everyone and thanks for the help in advance. I am developing a SignalR .Net 5 MVC application that uses SignalR to notify clients of inbound calls logged to a database. I am using the javascript sdk to perfrom client notifications. I ahve also confirmed the SQL Server Service Broker is enabled and working. Here is my controller code:
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();
}
}
The javascript notification script:
"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());
});
On the initial page load, the notification works as expected and writes a response to the console. However, when the database updates, the email notifications fire, however, the clients do not update. I stuck as to where to go from here. Any help would be appreciated.
