Worker Services in ASP.NET Core
Worker Services is a new feature in ASP.Net Core that enables you to write long-running background tasks with ease. This feature allows you to create and run a background service that doesn't interfere with your web application's lifetime or request/response cycle.
In this page, we will discuss what Worker Services are, how to create them in ASP.Net Core, and how to use them effectively.
Syntax
To create a Worker Service in ASP.Net Core, you need to create a class that implements the IHostedService
interface. This interface has two methods, StartAsync()
and StopAsync()
, that you need to implement.
Here is an example of a Worker Service class:
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
public class ExampleWorker : IHostedService
{
private Task _executingTask;
private CancellationTokenSource _cancellationTokenSource;
public Task StartAsync(CancellationToken cancellationToken)
{
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_executingTask = Execute(_cancellationTokenSource.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
if (_executingTask == null)
{
return;
}
_cancellationTokenSource.Cancel();
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
cancellationToken.ThrowIfCancellationRequested();
}
private async Task Execute(CancellationToken cancellationToken)
{
// Do your background work here
// You can check the cancellationToken to see if your service was cancelled
}
}
Example
To use the Worker Service in your ASP.Net Core application, you need to register it with the IServiceCollection
. This is typically done in the Startup.cs
file.
public void ConfigureServices(IServiceCollection services)
{
// Add the worker service
services.AddHostedService<ExampleWorker>();
//...
}
Explanation
A background service is a long-running task that runs independently of the main ASP.NET Core application. It is started when the application is started and stopped when the application is stopped. The worker service is just one type of background task that can run in the background in an ASP.Net Core application.
Worker services can be used to perform tasks like background processing, sending emails, refreshing caches, or performing scheduled tasks.
Use
The IHostedService
interface provides you a simple and easy way to create and run long-running background tasks in ASP.Net Core without interfering with the main application's lifetime. You can use it to perform tasks that don't need to be done in real-time, such as syncing data from a third-party service.
Important Points
- A Worker Service is a background service that is started when the application starts and stopped when the application stops.
- The
IHostedService
interface provides two methods to implement:StartAsync()
andStopAsync()
. - Worker services can be used to perform tasks like background processing, sending emails, refreshing caches, or performing scheduled tasks.
Summary
In this page, we discussed what Worker Services are and how to create them in ASP.Net Core. We covered the syntax, example, explanation, use cases, and important points regarding Worker Services. Worker Services are an excellent way to perform continuous processing in your ASP.Net Core application without affecting its main processes.