如何使用.net core 3.1 SignalR用于存储过程
How to use .net core 3.1 SignalR uses for stored procedure
我想将 SignalR 用于存储过程。我想做 运行 实时存储过程。
页面需要刷新。但是我不知道 运行 我的存储过程在哪里。
如果你能帮忙,我会很高兴。这对我很重要。
我的是使用 EFCore 的 SignalR。您可以 运行 TestSignalR() 函数中的存储过程。
Startup.cs
添加 services.AddSignalR() 和 app.UseSignalR()。
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSignalR(routes =>
{
routes.MapHub<YourSignalHub>("/yoursignalhub");
});
}
为集线器创建新的 class。 YourSignalHub.cs
public class YourSignalHub : Hub
{
public async Task SendWhatever()
{
await Clients.All.SendAsync("GetSomething");
}
}
YourPage.cshtml
<a id="btnSend">Send</a>
<span id="spanLiveMessage"></span>
<script>
var connection = new signalR.HubConnectionBuilder().withUrl("/yoursignalhub").build();
connection.on("GetSomething", function () {
$.post('@Url.Action("TestSignalR", "Home")', null, function (e) {
$("#spanLiveMessage").text("test");
// or your code
});
});
$("#btnSend").click(function () {
connection.invoke("SendWhatever").catch(function (err) {
return console.error(err.toString());
});
});
</script>
HomeController.cs
[HttpPost]
public IActionResult TestSignalR()
{
// your code to execute stored procedure here
}
是的,这对我有用。但是我在转换数据时出错了。因为
模型未定义。 JSON 序列化它解决了我的问题。
public async Task<string> UpdateCustomerList() { branch_id = Convert.ToInt32(_httpContextAccessor.HttpContext.User.FindFirstValue("branch_id ")); if (branch_id != 0) { customerViewModelsList = _uow.customers.GetLiveCustomers(sube_id); } return JsonConvert.SerializeObject(customerViewModelsList); }
我想将 SignalR 用于存储过程。我想做 运行 实时存储过程。
页面需要刷新。但是我不知道 运行 我的存储过程在哪里。 如果你能帮忙,我会很高兴。这对我很重要。
我的是使用 EFCore 的 SignalR。您可以 运行 TestSignalR() 函数中的存储过程。
Startup.cs
添加 services.AddSignalR() 和 app.UseSignalR()。
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSignalR(routes =>
{
routes.MapHub<YourSignalHub>("/yoursignalhub");
});
}
为集线器创建新的 class。 YourSignalHub.cs
public class YourSignalHub : Hub
{
public async Task SendWhatever()
{
await Clients.All.SendAsync("GetSomething");
}
}
YourPage.cshtml
<a id="btnSend">Send</a>
<span id="spanLiveMessage"></span>
<script>
var connection = new signalR.HubConnectionBuilder().withUrl("/yoursignalhub").build();
connection.on("GetSomething", function () {
$.post('@Url.Action("TestSignalR", "Home")', null, function (e) {
$("#spanLiveMessage").text("test");
// or your code
});
});
$("#btnSend").click(function () {
connection.invoke("SendWhatever").catch(function (err) {
return console.error(err.toString());
});
});
</script>
HomeController.cs
[HttpPost]
public IActionResult TestSignalR()
{
// your code to execute stored procedure here
}
是的,这对我有用。但是我在转换数据时出错了。因为 模型未定义。 JSON 序列化它解决了我的问题。
public async Task<string> UpdateCustomerList() { branch_id = Convert.ToInt32(_httpContextAccessor.HttpContext.User.FindFirstValue("branch_id ")); if (branch_id != 0) { customerViewModelsList = _uow.customers.GetLiveCustomers(sube_id); } return JsonConvert.SerializeObject(customerViewModelsList); }