Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement background services for an ASP.NET application?

My ASP.NET application needs a number of supporting services to run periodically in the background. For example:

  • I need to query the database (or cache) every 1-5 minutes, identify overdue work items and notify users by email
  • I need to generate nightly reports that are then emailed to subscribers
  • Plus other system/non-user admin tasks

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.

like image 647
flesh Avatar asked Nov 13 '08 21:11

flesh


People also ask

What is background service in ASP.NET Core?

BackgroundService is a base class for implementing a long running IHostedService. ExecuteAsync(CancellationToken) is called to run the background service.

How do you call a background task with hosted service from .NET Core Web API?

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.

What is background jobs C#?

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.

What is IHostedService in .NET Core?

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 ).


2 Answers

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.

like image 136
Nelson Miranda Avatar answered Sep 18 '22 23:09

Nelson Miranda


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

like image 23
deadbug Avatar answered Sep 18 '22 23:09

deadbug