22 Building background tasks and services
This chapter covers
- Creating tasks that run in the background for your application
- Using the generic IHost to create Windows Services and Linux daemons
- Using Quartz.NET to run tasks on a schedule, in a clustered environment
We’ve covered a lot of ground in the book so far. You’ve learned how to create page-based applications using Razor Pages and how to create APIs for mobile clients and services. You’ve seen how to add authentication and authorization to your application, how to use EF Core for storing state in the database, and how to create custom components to meet your requirements.
As well as these “UI” focused apps, you may find you need to build “background” or “batch-task” services. These services aren’t meant to interact with users directly. Rather they stay running in the background, processing items from a queue or periodically executing a long-running process.
For example, you might want to have a background service that sends email confirmations for eCommerce orders, or a batch job that calculates sales and losses for retail stores after the shops close. ASP.NET Core includes support for these “background tasks” by providing abstractions for running a task in the background when your application starts.