locked
Problem with SqlNotification in SignalR RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for the help in advance.  I am developing a .Net 5 SignalR notification that utilizes SQL Notification.  The application is partially working, sending a notification to the client on the first time the page loads.  However, subsequent database update fire the OnChange event handler, but doesn't send the update back to the client.  Here is my hub code:

        public class CallNotificationHub : Hub
        {
            //
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-5.0
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-5.0
            //
            private SqlDependency dependency;
    
            //  public Task OnConnected()
            public override async Task OnConnectedAsync()
            {
                //return base.OnConnectedAsync();
                await base.OnConnectedAsync();
            }
    
    
            //[HubMethodName("sendCallNotifications")]
            public async Task SendCallNotifications()
            {
                SendEmail sendEmail = new SendEmail("SendCallNotifications Reached", "New SignalR5 Notification", "");
    
                try
                {
                    string connectionString = "connectionstring";
                    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();
    
                    //await Clients.All.SendAsync("ReceiveMessage", "user", "message");
                }
                catch (Exception ex)
                {
                    SendEmail sendErrEmail = new SendEmail("SendCallNotifications Error", ex.ToString(), "");
                }
                
                await Clients.All.SendAsync("RecieveNotification", "Changed");
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {
                SendEmail sendEmail = new SendEmail("Dependency Change", e.Type.ToString(), "");
                if (e.Type == SqlNotificationType.Change)
                {                
                    CallNotificationHub nHub = new CallNotificationHub();
                    nHub.SendCallNotifications();
                }
            }

    The client code:

    "use strict";
    
    var connection = new signalR.HubConnectionBuilder().withUrl("/CallNotificationHub").build();
    
    connection.on("RecieveNotification", (result) => {
        var li = document.createElement("li");
        li.textContent = result;
        document.getElementById("messagesList").appendChild(li);
    
        console.log(result);
        alert("Changed");
    });
    
    connection.start().then(function () {
        connection.invoke("SendCallNotifications").catch(err => console.error(err));
    }).catch(function (err) {
        return console.error(err.toString());
    });

    Any help would be appreciated.

    Monday, May 24, 2021 12:54 AM

All replies

  • User1122355199 posted

    Update.  After some further debugging.  I determined the SendCallNotifications method is called correctly upon each database update, however, the client does not update correctly.  I'm not sure if the problem lies in my javascript, a threading problem since it is set up as a task, or whether there is some type of failure in the pipeline.  Any help would be appreciated.

    Tuesday, May 25, 2021 5:26 PM
  • User1122355199 posted

    Just ran into a new error written to the console that I haven't seen before:

    Error: An unexpected error occurred invoking 'SendCallNotifications' on the server.
        invocationDescriptor.invocationId http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:2259
        processIncomingData http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:2371
        onreceive http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1923
        onmessage http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4728
        connect http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4725
        connect http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4687
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4649
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4630
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4624
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4620
        connect http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:4669
        startTransport http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3842
        createTransport http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3792
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3411
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3392
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3386
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3382
        createTransport http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3745
        startInternal http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3665
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3411
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3392
        fulfilled http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3383
        promise callback*step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3385
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3386
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3382
        startInternal http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3596
        start http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3480
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3411
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3392
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3386
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3382
        start http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:3468
        startInternal http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:2036
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1883
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1864
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1858
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1854
        startInternal http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:2024
        startWithStateTransitions http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:2006
        step http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1883
        verb http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1864
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1858
        __awaiter http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1854
        startWithStateTransitions http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1993
        start http://192.168.0.3:61443/js/signalr/dist/browser/signalr.js:1989
        <anonymous> http://192.168.0.3:61443/js/CallNotification2.js:14
    CallNotification2.js:15:69
    

    Not sure what it is telling me.

    Wednesday, May 26, 2021 3:34 PM