Key Concepts:
- IHost: Initializes and configures services for the application.
- IHostedService: Represents background services with lifecycle management.
- Graceful Shutdown: Handling cleanup tasks when the application stops.
In .NET Core, the IHost interface is used to configure and run applications, particularly when implementing background services or hosting long-running processes. By using IHostedService, developers can easily manage these services with built-in support for dependency injection, logging, and graceful shutdown.
In .NET 6 and 7, background services (IHostedService) are often used for tasks like long-running processes. These services run in the background and might need to access scoped services (e.g., DbContext). Directly injecting a scoped service into IHostedService causes issues because they have different lifetimes.
To solve this, we can use IServiceScopeFactory to create a scope for each background task. This allows us to resolve the scoped service properly within the background service, ensuring it’s disposed correctly.
Here’s an updated example of how to implement this in a modern .NET application:
Continue reading “Injecting a Scoped Service into IHostedService in .NET 6/7”In the previous blog post called background tasks with ASP.NET Core using the IHostedService Peter described how to use the IHostedInterface for background tasks. In this post, we continue on this subject and add some pointers on how to perform scheduled background tasks.
In many software projects, there are repetitive tasks; some do just repeat every x seconds after the last instance is finished but you might also have to run a task on a schedule like every 10 minutes. When building repeating or scheduled tasks there are many options on how to approach the scheduling and this approach can be influenced by a number of technical choices.
Building the scheduling yourself is an option when you do not want to add extra dependencies to your project, have full control or just want an extra technical challenge. An out of the box solution you can a look at Hangfire, Quartz.net, or an external service that does an http call every x seconds to trigger the task (something like Pingdom).
Continue reading “Run scheduled background tasks in ASP.NET Core”
Run background processes with the IHostedService and how to inject your services with dependency injection
Many services need background processing. The ASP.NET Core 2.X IHostedService interface gives you an easy implementation skeleton to implement background processes. The Hosted Services are registered in the dependency injection at startup and started automatically. You do not have to do the pluming to get them started at startup. On shutdown you can implement a graceful shutdown. When running background processes there a few pitfalls to avoid. In this blog I’ll introduce the IHostedService and how to avoid common memory leaks when implementing the hosted service.
Continue reading “ASP.NET Core background processing with IHostedService”