Asp.net core2.2 webapi,SQL依赖项更改的事件处理程序不会再触发一次

问题描述 投票:0回答:1

我有一个简单的项目,我想在其中实现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 Server向表中插入行时,仅触发一次TradeInfoDependency_OnChange(),这是第一次插入,而依赖状态等于“ e.Info == SqlNotificationInfo.Insert”。不幸的是,其余的行插入将不会触发处理程序。如何解决每次插入触发的问题?

asp.net-core sql-insert eventhandler sqldependency webapi
1个回答
0
投票

作为“ @Nan Yu”,我不得不回想一下RegisterTradeInformationNotification()函数

© www.soinside.com 2019 - 2024. All rights reserved.