Reputation: 466
What am I doing wrong?
IHostedService classes are registered in ASP.net core HostBuilder.
Then run continuously in the background, even if they have no work to do.
There is no way to pass them work, so I suppose they must pick up tasks from a dynamic store eg database
So it seems they poll mostly. Or run short interval timers; looking for work
Then when they get jobs they can only do 1 at a time.
So if my users (100+) run reports on a Friday (or what ever day they wish) the service just polls the database for 6.5 days and then is throttled for 0.5 days to get the 100+ reports generated.
So how can I
Furthur to this I will need 10+ different types of IHostedService. (10+ different polling types)
So running them in the backgorund just to poll the database takes up CPU cycles on both web server and database server
Upvotes: 0
Views: 668
Reputation: 25350
control the starting of the IHostedservice service
You can't. The start and stop of IHostedService
is controlled by Host itself. See official docs
Run more that 1 instance of the IHostedservice service
As the IHostedService
is run by host itself, I believe you won't actually want to start multiple IHostedService
, but instead you're seeking a way to start tasks in parallel way and then wait all the tasks to be done. IMO, a better way is to create a delegate that returns a task which consists of several sub tasks. This sub tasks will run in a parallel way.
Send tasks in the form of data to the IHostedservice service (instance)
The official docs has an excellent example for this:
If you want to start several work items at the same time, just dequeue multiple work items and then start these tasks with theTask.WhenAll :
await Task.WhenAll(...);
For more details, see parallel-programming
Upvotes: 1