In addition, you can read about using ASP.NET Core hosted services to run a background task.
Download an ASP.NET Core hosted service which is written in .NET 6.
The code demonstrates how to set up a hosted service, either by inheriting the IHostedService
interface, or the BackgroundService
abstract class. It also has sample code to fire off a task in a background service.
The background services have been added to the DI container through the IServiceCollection
interface, and use dependency injection using the service lifetimes available.
This is the software that will need to be installed onto your machine.
These are the steps to get the application working.
You are now ready to go.
Open up RoundTheCode.HostedServiceExample.sln
in Visual Studio 2022.
Start the project in Visual Studio.
The hosted services will start once the project has been launched. See the console application as it sends information through the ILogger
instance in the hosted service.
Hit https://localhost:8002
to test that the web application is working.
Watch our video where we create and configure hosted services in an ASP.NET Core web application.
In addition, you can read about using ASP.NET Core hosted services to run a background task.