ASP.NET核心后台服务通过SignalR发送数据 [英] ASP.NET core background service to send data through 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??
推荐答案
您可以使用IHostedService
和IHubContext
来完成此操作.
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屋!