【问题标题】:Problems with SignalR Core and SQL NotificationSignalR Core 和 SQL 通知的问题
【发布时间】: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 聊天应用程序。

标签: asp.net-core-signalr


【解决方案1】:

尝试修改您的代码,如下所示。它对我有用。

using Microsoft.AspNetCore.SignalR;
using signalr.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace signalr.Hubs
{
    public class CallNotificationHub : Hub, ICallNotificationHub
    {
        public override async Task OnConnectedAsync()
        {

            await base.OnConnectedAsync();
        }

        public async Task SendCallNotifications()
        {

            try
            {
                string connectionString = @"Data Source=.************ue; Connection Timeout=30;";
                SqlDependency.Start(connectionString);
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();

                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT *FROM [dbo].[Restaurant]";
                command.Connection = connection;
                command.CommandType = CommandType.Text;

                var dependency = new SqlDependency(command);
                counter = 0; //Whenewer the web method is called, set te counter to 0
                //dependency.OnChange -= new OnChangeEventHandler(dependency_OnChange);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                DataTable dt = new DataTable();

                dependency.AddCommandDependency(command);

                var reader = command.ExecuteReader();

                Console.WriteLine("SendCallNotifications Reached"+ "New SignalR5 SendCallNotifications at " + DateTime.Now.ToString());
                //SendEmail sendEmail = new SendEmail("SendCallNotifications Reached", "New SignalR5 SendCallNotifications at " + DateTime.Now.ToString(), "");

            }
            catch (Exception ex)
            {
                Console.WriteLine("SendCallNotifications Error " + DateTime.Now.ToString()+ ex.ToString());
                //SendEmail sendErrEmail = new SendEmail("SendCallNotifications Error", ex.ToString(), "");
            }

            await Clients.All.SendAsync("RecieveNotification", "Changed at " + DateTime.Now.ToString());
            Console.WriteLine("RecieveNotification " + "Changed at " + DateTime.Now.ToString());

        }
        public int counter = 0;
        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
       

            if (e.Type == SqlNotificationType.Change && e.Info == SqlNotificationInfo.Update && counter == 0)
            {
                //Call SignalR  
                Console.WriteLine("Dependency Change " + e.Type.ToString() + DateTime.Now.ToString());
                //SendEmail sendEmail = new SendEmail("Dependency Change", e.Type.ToString() + " at " + DateTime.Now.ToString(), "");
                CallNotificationHub nHub = new CallNotificationHub();
                nHub.SendCallNotifications();
                counter++; //The update is done once
            }
            else
            {
                counter = 0; //if the update is needed in the same iteration, please don't update and set the counter to 0
            }

        }
    }
}

【讨论】:

  • 这让我回到了原来的帖子。我认为我的问题出在 javascript 更新代码中,但不确定。您是在接收浏览器的实际更新,还是只是从控制器接收通知。
  • @Greybeard 试试看。
  • 我做到了。使用 VS 2019、SQL Server 2012 和 IIS Express 在笔记本电脑上进行了尝试。客户端仅在第一页加载时更新,但控制器发送通知然后移动到生产机器,Windows Server 2012,SQL Server 2012。结果与测试机器相同。警报触发 0n 第一页加载。第一次加载后,只有电子邮件通知。
  • 有没有人有一个真正工作的带有 SQL 背板的 SignalR Core?我显然接近让这个工作,但没有找到任何例子。这不再受支持了吗?
  • @Greybeard 可以安排时间吗,我可以和你聊天。
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多