是否有任何可用于使用SignalR用的SqlDependency核心2.1样本。也使券商等,但从来没有得到任何依赖onChange事件触发。就在事件订阅被触发。当在后端的MS-SQL数据库表城市的变化,我想看到的变化反映了客户端网页上右走,而不必刷新/重新加载页面。
//启动的依赖性时在ConfigureServices SqlDependency.Start(Configuration.GetConnectionString( “DefaultConnection”))的应用程序启动;
using Microsoft.AspNetCore.SignalR;
using SignalR_Test4.Data;
using SignalR_Test4.Hubs;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace SignalR_Test4.Models
{
public class CityRepository
{
private readonly ApplicationDbContext _context;
private readonly IHubContext<CityHub> _hubcontext;
public CityRepository(ApplicationDbContext context, IHubContext<CityHub> hubcontext)
{
_context = context;
_hubcontext = hubcontext;
}
public IEnumerable<City> GetCities()
{
List<City> listOf = new List<City>();
//listOf = _context.Cities;
using (var conn = new SqlConnection(GlobalVar.connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(@"SELECT * FROM Cities", conn))
{
cmd.Notification = null;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += Dependency_OnChange;
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
listOf.Add(new City { Id = (string)reader["Id"], Name_en = (string)reader["name_en"], CountryId = (string)reader["CountryId"], Code = (string)reader["Code"] });
}
}
}
return listOf;
}
private void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
_hubcontext.Clients.All.SendAsync("GetCities");
}
}
}
}
问题是行内:
VAR CMD =新的SqlCommand(@ “SELECT标识,Name_en,CountryId,代码从[DBO] .Cities”,康涅狄格州)
据需要使用的字段名称(而不是*)以及还有2部分表名称约定=> [DBO] .Cities