Asp.net 核心 2.2 web api,SQL 依赖项更改事件处理程序仅触发一次,不再触发
Asp.net core2.2 webapi, SQL dependency changed event handler gets triggered only once not more
我有一个简单的项目,我想在其中实现 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 服务器向 table 中插入行时,只有一次 TradeInfoDependency_OnChange() 被触发,这是第一次插入,而依赖状态等于"e.Info == SqlNotificationInfo. Insert"。
不幸的是,其余的行插入不会触发处理程序。我该如何解决它以触发每次插入?
正如“@Nan Yu”提到的,我不得不回忆起 RegisterTradeInformationNotification() 函数
我有一个简单的项目,我想在其中实现 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 服务器向 table 中插入行时,只有一次 TradeInfoDependency_OnChange() 被触发,这是第一次插入,而依赖状态等于"e.Info == SqlNotificationInfo. Insert"。 不幸的是,其余的行插入不会触发处理程序。我该如何解决它以触发每次插入?
正如“@Nan Yu”提到的,我不得不回忆起 RegisterTradeInformationNotification() 函数