Last post Aug 14, 2019 05:14 PM by bruce (sqlwork.com)
Aug 13, 2019 09:02 AM|Hulkstance|LINK
I'm making an application which is supposed to have multiple threads/services (bots) running. Those threads are supposed to check an API for current currency values. There will be some calculations on each rounded hour (e.g. 10:00, 11:00, etc).
Currently, I'm running Angular (front-end) and ASP.NET Core 2.2 MVC + SignalR (back-end).
What's the right way of hosting those threads/services? Assuming those threads are spawned based on database records.
I was looking at
https://docs.microsoft.com/en-us/aspnet/core/signalr/background-services?view=aspnetcore-2.2 but I'm not quite if that's the right solution since I have to display the results in the front-end with SignalR if they match to certain conditions.
What's the right way of doing that scenario?
Aug 13, 2019 12:17 PM|maherjendoubi|LINK
You can use workers but it is supported in .NET Core 3.0 beginning from preview 3. The current version of .NET Core 3.0 is preview 7 at the moment of writing this message.
Aug 13, 2019 07:01 PM|Hulkstance|LINK
Aug 13, 2019 08:20 PM|maherjendoubi|LINK
Thank you for your answer! Is there anything else included in .NET Core 2.2?
Aug 14, 2019 05:52 AM|Rena Ni|LINK
You could also refer to the background tasks with hosted service:
And if you want to send the calculation data to clients through signalR, you could refer to:
Aug 14, 2019 03:18 PM|Hulkstance|LINK
Thank you both, Rena and Maher!
Timed background tasks fits me. The problem is that when I register the service using
services.AddHostedService<BotHostedService>();, it immediately starts it, which doesn't fit my needs. I need a scoped service that can be injected into my SignalR hub but multiple threads of it. That's like having 5 threads of
BotHostedService and each of them implementing Timer class exactly like
in Timed background tasks' example code. I saw an example scoped service code at that link:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2&tabs=visual-studio#consuming-a-scoped-service-in-a-background-task but I want multiple threads of it. Is this doable somehow?
Aug 14, 2019 04:20 PM|maherjendoubi|LINK
Scoped services are created per scope. In a web application, every web request creates a new separated service scope. That means scoped services are generally created per web request which is suitable for your need. But you
should use scoped service lifetime
carefully since it can be tricky if you create child service scopes or use these services from a non-web application.
Aug 14, 2019 05:14 PM|bruce (sqlwork.com)|LINK
when using background services, you use Scope when you are using DI and you need a new instance. This is unrelated to the scope created for web requests.
you should be looking at the queued sample. your signal/r client should queue a timer request. the background process should be running on a timer. every time it hits the clock cycle, it should check the queue / collection and process the queue / collection
(starting threads if required). you will then need a determine when to stop processing and remove the queue / collection.