Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core background service to send data through 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.

I planned to use

System.Threading.Thread.Sleep(3000);

with

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??

like image 461
Amith Avatar asked Oct 11 '25 20:10

Amith


1 Answers

You can use an IHostedService and IHubContext to accomplish this.

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

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

See the doc on IHubContext:

https://learn.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

And the wire up:

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

like image 93
davidfowl Avatar answered Oct 14 '25 10:10

davidfowl