Wednesday, 22 July 2026

Using ASP.NET Core's BackgroundService for Prolonged Tasks

Leave a Comment

An application shouldn't execute all of its tasks as part of an HTTP request. While certain processes must run continually in the background, others take a long time to finish. Email processing, report generation, data synchronization, temporary file cleanup, and message consumption from a queue are a few examples.


Your application may become slower and provide a worse user experience if these actions are executed directly within a controller or API endpoint. To address this issue, ASP.NET Core offers the BackgroundService class.

With BackgroundService, you can maintain the responsiveness and scalability of your application while doing lengthy activities independently of incoming HTTP requests.

This post will teach you how to utilize BackgroundService, when to use it, and how to create dependable background processes in ASP.NET Core.

What Is BackgroundService?

BackgroundService is an abstract class provided by ASP.NET Core for implementing hosted services that run in the background.

Unlike a controller, a background service starts when the application starts and continues running until the application stops.

Some common use cases include:

  • Sending emails

  • Processing message queues

  • Importing data from external systems

  • Scheduled cleanup tasks

  • Generating reports

  • Monitoring application health

  • Processing uploaded files

Since these operations run independently, they don't block incoming user requests.

Creating a Background Service

Creating a background service is simple. Create a class that inherits from BackgroundService.

using Microsoft.Extensions.Hosting;

public class WorkerService : BackgroundService
{
    protected override async Task ExecuteAsync(
        CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            Console.WriteLine("Background task is running...");

            await Task.Delay(5000, stoppingToken);
        }
    }
}

The ExecuteAsync method contains the logic that runs continuously until the application shuts down.

Registering the Background Service

After creating the service, register it with the dependency injection container.

builder.Services.AddHostedService<WorkerService>();

When the application starts, ASP.NET Core automatically starts the background service.

No additional configuration is required.

Understanding the Cancellation Token

A background service should always respond gracefully when the application is shutting down.

ASP.NET Core provides a CancellationToken that signals when the service should stop.

Example:

while (!stoppingToken.IsCancellationRequested)
{
    await ProcessDataAsync();

    await Task.Delay(10000, stoppingToken);
}

Checking the cancellation token ensures that the application can shut down cleanly without leaving unfinished operations.

Processing Queue Messages

One of the most common uses of BackgroundService is processing messages from a queue.

A typical workflow looks like this:

  1. A user submits a request.

  2. The application places a message in a queue.

  3. The API immediately returns a response.

  4. The background service reads the message.

  5. The task is processed asynchronously.

This approach improves responsiveness because users don't have to wait for lengthy operations to complete.

Using Dependency Injection

Background services can use other application services through dependency injection.

For example:

public class WorkerService : BackgroundService
{
    private readonly ILogger<WorkerService> _logger;

    public WorkerService(ILogger<WorkerService> logger)
    {
        _logger = logger;
    }

    protected override async Task ExecuteAsync(
        CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker is running.");

            await Task.Delay(5000, stoppingToken);
        }
    }
}

This makes it easy to use logging, database services, or other application components inside the background service.

Handle Exceptions Properly

A background service should never stop unexpectedly because of an unhandled exception.

Instead, catch exceptions, log them, and continue processing when appropriate.

try
{
    await ProcessOrdersAsync();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Proper exception handling improves application reliability and simplifies troubleshooting.

Avoid Blocking Operations

Background services should use asynchronous methods whenever possible.

Instead of:

Thread.Sleep(5000);

Use:

await Task.Delay(5000, stoppingToken);

Asynchronous operations free application threads to handle other work, improving scalability.

Real-World Example

Imagine an online shopping application.

When a customer places an order:

  1. The API saves the order.

  2. A message is added to a queue.

  3. The API immediately returns a success response.

  4. A background service processes the order.

  5. The service sends a confirmation email.

  6. Inventory is updated.

  7. A shipping request is created.

The customer receives an immediate response while the remaining tasks continue in the background.

Best Practices

When using BackgroundService in ASP.NET Core, follow these recommendations:

  • Keep background tasks independent of HTTP requests.

  • Always use the provided CancellationToken.

  • Prefer asynchronous operations over blocking calls.

  • Handle exceptions to prevent unexpected service termination.

  • Use dependency injection instead of creating services manually.

  • Log important events and errors for easier monitoring.

  • Avoid performing CPU-intensive work on the main application thread.

  • Monitor background service performance and resource usage in production.

Conclusion

BackgroundService provides a clean and reliable way to run long-running tasks in ASP.NET Core applications. By moving work such as email processing, queue consumption, report generation, and scheduled maintenance outside the request pipeline, you can improve application responsiveness and deliver a better user experience.

When combined with dependency injection, asynchronous programming, and proper error handling, BackgroundService becomes a powerful tool for building scalable and maintainable applications. By following the best practices outlined in this article, you can create background processes that run efficiently and reliably in production environments.

ASP.NET Core 10.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 10.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core 10.0, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFE guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment