ASP.NET Core hosted service in .NET 6

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.

Software

This is the software that will need to be installed onto your machine.

  • Visual Studio 2022 17.0.4 or above. It will work with the free community version. Please note, this project will not work with VS 2019.
  • .NET 6.0 SDK 6.0.101 or above. Visual Studio 2022 should already have the .NET SDK installed.

Get the application working

These are the steps to get the application working.

  • Fill out the source code form. We will send you an email where you can download the source code.

You are now ready to go.

Open the project in Visual Studio

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.

More information

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.

Free .NET videos

  • Do you want to watch free videos featuring .NET 7 new features?
  • How about what's new in C# 11?
  • Or a recap on the SOLID principles?
Watch our .NET videos