My ASP.NET application needs a number of supporting services to run periodically in the background. For example:
What is the best way to implement these services? Should I include them as part of the web application, starting an instance of each 'service' in Application_Start()? Or create them as actual standalone services? If they were part of the web application I can potentially utilize my cached data store, but are there any downsides to this approach?
Thanks for any advice.
BackgroundService is a base class for implementing a long running IHostedService. ExecuteAsync(CancellationToken) is called to run the background service.
In ASP.Net core, we can implement IHostedService interface to run background tasks asynchronously inside our application. It provides to two methods “StartAsync” and “StopAsync”. as the name suggested these methods is used to start and stop the task.
A background job is a class that implements the IBackgroundJob<TArgs> interface or derives from the BackgroundJob<TArgs> class. TArgs is a simple plain C# class to store the job data. This example is used to send emails in background.
The IHostedService interface provides a convenient way to start background tasks in an ASP.NET Core web application (in . NET Core 2.0 and later versions) or in any process/host (starting in . NET Core 2.1 with IHost ).
This year I needed to implement a single task to cache some data from a database, It had to verify a changing code every n minutes, I found this very nice article;
Simulate a Windows Service using ASP.NET to run scheduled jobs
I did it that way and it worked, but as I told you was it was only a single task, but if you see the example above you can see that it performs several tasks.
As you could see, in the cache you can perform a task at any time and on intervals, the nightly task could be done at midnight, the others every n minutes or hours.
Hope it helps.
-- For the nightly report generation I would look into SQL Reporting Services because they have some native subscription services that allow you to e-mail reports to users with virtual no custom code.
-- For the overdue work item notifications you could either use SSRS (mentioned above) by sending them a report with their overdue items or, if you're running SQL 2k5, use Notification Services.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With