ASP.NET核心后台服务通过SignalR发送数据 [英] ASP.NET core background service to send data through signalR

查看:180
本文介绍了ASP.NET核心后台服务通过SignalR发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用signalR从服务器向客户端发送数据.我想要的是检查表(MSSQL DB)中的数据,并通过signalR将相关数据发送到客户端.因此,我想创建一些后台服务来检查数据库并发送数据.

I'm using signalR to send data to clients from server. What I want to is check data in the table(MSSQL DB) and send the related data to clients through signalR. So I want to create some background service to check database and send data.

我打算使用

System.Threading.Thread.Sleep(3000);

while(true) 

循环.如何创建服务类以在启动时运行此方法.

loop. How can I create service class to run this method in startup.

是否可以使用标准方法来执行此任务?

Is there any method to use do this task in standard way??

推荐答案

您可以使用IHostedServiceIHubContext来完成此操作.

You can use an IHostedService and IHubContext to accomplish this.

请参阅有关使用ASP.NET Core进行后台服务的文档:

See the doc on background services with ASP.NET Core:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1

请参阅IHubContext上的文档:

https://docs.microsoft. com/en-us/aspnet/core/signalr/hubcontext?view = aspnetcore-2.1

这是将两者一起使用的示例:

Here's an example of using both together:

> https://github.com/davidfowl/UT3/blob/fb12e182d42d2a5a902c1979ea0e91b66fe60607/UTT/Scavenger.cs

然后连线:

https://github.com/davidfowl/UT3/blob/fb12e182d42d2a5a902c1979ea0e91b66fe60607/UTT/Startup.cs#L46

这篇关于ASP.NET核心后台服务通过SignalR发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆