0

我有一个简单的项目,我想在其中实现 SQL 依赖项。我的问题是,当我在数据库中插入一行时,只有一次 SQL 依赖项的更改事件处理程序被触发,而不再触发。

在 startup.cs 我放了这个:

public Startup(IConfiguration configuration)
    {
        Configuration = configuration; 
        string cs = "Data Source=.;Initial Catalog=signalr;Integrated Security=True;user id=sa;password=xyz";

        SqlDependency.Start(cs);
    }

我的控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;

namespace SignalrChat.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ChatController : ControllerBase
    {
        [Route("get")]
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            RegisterTradeInformationNotification().Wait();
            return Ok("dasdasd");
        }

        private async Task<object> RegisterTradeInformationNotification()
        {
            IEnumerable<object> list;
            try
            {
                string cs = "Data Source=.;Initial Catalog=signalr;Integrated Security=True;user id=sa;password=xyz";

                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    string cmdText = @"SELECT [Id],[Message] FROM [dbo].[Chat]";
                    using (SqlCommand cmd = new SqlCommand(cmdText, con))
                    {
                        cmd.Notification = null;


                        SqlDependency tradeInfoDependency = new SqlDependency(cmd);
                        tradeInfoDependency.OnChange += TradeInfoDependency_OnChange;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }

                        SqlDataReader reader = cmd.ExecuteReader();
                        list = reader.Cast<IDataRecord>()
                            .Select(x => new
                            {
                                Id = (int)reader["Id"],
                                Message = (string)reader["Message"]
                            }).ToList();

                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return Ok(new { success = list });
        }

        private void TradeInfoDependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            var dependency = sender as SqlDependency;

            if (dependency == null) return;

            if (e.Info == SqlNotificationInfo.Insert)
            {


                //  _hubContext.Clients.All.SendAsync("TradeInfo");
            }

        }
    }

当我从 SQL 服务器在表中插入行时,只有一次 TradeInfoDependency_OnChange() 被触发,这是第一次插入,而依赖状态等于“e.Info == SqlNotificationInfo.Insert”。不幸的是,其余的行插入不会触发处理程序。我如何解决它以触发每个插入?

4

1 回答 1

0

正如“@Nan Yu”提到的,我不得不回忆一下 RegisterTradeInformationNotification() 函数

于 2020-01-07T06:58:01.873 回答