使用 SignalR、ASP.NET 核心和 SQL 依赖注入在浏览器中显示实时数据库更改
Show real time database changes in browser using SignalR, ASP.NET Core and SQL dependency injection
我是 SignalR 和 ASP.NET Core 的新手,我的根本问题是我希望用户能够实时查看对数据库所做的更改。我有一个使用 SignalR 和 ASP.NET 框架的工作代码示例,但是当我尝试将它转换为 ASP Core 时,由于我的集线器,它失败了。我很确定问题很简单,但网上的其他一些例子超出了我目前的深度。
CusHub.cs
:
public class CusHub : Hub
{
public static void Show()
{
// The below code is what doesn't work in aspcore.
// This is what i'm struggling with.
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<CusHub>();
context.Clients.All.displayCustomer();
}
}
CustomerController.cs
:
public JsonResult Get()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [Id],[CustId],[CustName] FROM [dbo].[ad.signalr] WHERE [Status] <> 0", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
SqlDataReader reader = command.ExecuteReader();
var listCus = reader.Cast<IDataRecord>()
.Select(x => new
{
Id = (int)x["Id"],
CusId = (string)x["CustId"],
CusName = (string)x["CustName"],
}).ToList();
return Json(new { listCus = listCus });
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
CusHub.Show();
}
客户观点:
@Html.Hidden("Get", Url.Action("Get", "Customer"))
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody id="tblInfo"></tbody>
</table>
SignalR js 文件:
$(function () {
var cus = $.connection.cusHub;
cus.client.displayCustomer = function () {
getData();
};
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tblInfo');
$.ajax({
url: $("#Get").val(),
type: 'GET',
datatype: 'json',
success: function (data) {
$tbl.empty();
$.each(data.listCus, function (i, model) {
$tbl.append(
'<tr>' +
'<td>' + model.Id + '</td>' +
'<td>' + model.CusId + '</td>' +
'<td>' + model.CusName + '</td>' +
'<tr>'
);
});
}
});
}
除了我的中心之外,代码没有抛出任何异常。
与:
GlobalHost.ConnectionManager.GetHubContext<CusHub>()
context.Clients.All.displayCustomer();
Iclientproxy does not contain a definition for displayCustomer();
我知道 SignalRCore 发生了一些变化,但是在 运行 有没有简单的修复方法来解决这个问题?
以上代码适用于ASP.NET框架
感谢您的帮助。
如果您有 displayCustomer()
方法,我假设您使用的是 Strongly typed hub。查看该文档中的示例,它应该会让您走上正确的道路。但这里有几个我马上看到的问题:
首先,您需要更改 CusHub
的声明以从 Hub<T>
派生,其中 T
是您为定义 SignalR 客户端将接受的方法而创建的接口.所以如果这叫做 ICusHub
,那么你会得到这样的东西:
public class CusHub : Hub<ICusHub>
其次,您不需要 context
。它已经在您的 Hub
class 中可用。所以你只需要这个:
await Clients.All.displayCustomer();
我是 SignalR 和 ASP.NET Core 的新手,我的根本问题是我希望用户能够实时查看对数据库所做的更改。我有一个使用 SignalR 和 ASP.NET 框架的工作代码示例,但是当我尝试将它转换为 ASP Core 时,由于我的集线器,它失败了。我很确定问题很简单,但网上的其他一些例子超出了我目前的深度。
CusHub.cs
:
public class CusHub : Hub
{
public static void Show()
{
// The below code is what doesn't work in aspcore.
// This is what i'm struggling with.
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<CusHub>();
context.Clients.All.displayCustomer();
}
}
CustomerController.cs
:
public JsonResult Get()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [Id],[CustId],[CustName] FROM [dbo].[ad.signalr] WHERE [Status] <> 0", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
SqlDataReader reader = command.ExecuteReader();
var listCus = reader.Cast<IDataRecord>()
.Select(x => new
{
Id = (int)x["Id"],
CusId = (string)x["CustId"],
CusName = (string)x["CustName"],
}).ToList();
return Json(new { listCus = listCus });
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
CusHub.Show();
}
客户观点:
@Html.Hidden("Get", Url.Action("Get", "Customer"))
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody id="tblInfo"></tbody>
</table>
SignalR js 文件:
$(function () {
var cus = $.connection.cusHub;
cus.client.displayCustomer = function () {
getData();
};
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tblInfo');
$.ajax({
url: $("#Get").val(),
type: 'GET',
datatype: 'json',
success: function (data) {
$tbl.empty();
$.each(data.listCus, function (i, model) {
$tbl.append(
'<tr>' +
'<td>' + model.Id + '</td>' +
'<td>' + model.CusId + '</td>' +
'<td>' + model.CusName + '</td>' +
'<tr>'
);
});
}
});
}
除了我的中心之外,代码没有抛出任何异常。
与:
GlobalHost.ConnectionManager.GetHubContext<CusHub>()
context.Clients.All.displayCustomer();
Iclientproxy does not contain a definition for displayCustomer();
我知道 SignalRCore 发生了一些变化,但是在 运行 有没有简单的修复方法来解决这个问题?
以上代码适用于ASP.NET框架
感谢您的帮助。
如果您有 displayCustomer()
方法,我假设您使用的是 Strongly typed hub。查看该文档中的示例,它应该会让您走上正确的道路。但这里有几个我马上看到的问题:
首先,您需要更改 CusHub
的声明以从 Hub<T>
派生,其中 T
是您为定义 SignalR 客户端将接受的方法而创建的接口.所以如果这叫做 ICusHub
,那么你会得到这样的东西:
public class CusHub : Hub<ICusHub>
其次,您不需要 context
。它已经在您的 Hub
class 中可用。所以你只需要这个:
await Clients.All.displayCustomer();