Customizing ASP.NET Core Part 5: HostedServices

DZone 's Guide to

Customizing ASP.NET Core Part 5: HostedServices

This article is really more about a feature you can use to create background services to run tasks asynchronously inside your application.

· Web Dev Zone ·
Free Resource

This fifth part of this series doesn't really show a customization. This part is more about a feature you can use to create background services to run tasks asynchronously inside your application. Actually, I use this feature to regularly fetch data from a remote service in a small ASP.NET Core application.

The Series's Topics

About HostedServcices

HostedServices are a new thing in ASP.NET Core 2.0 and can be used to run tasks asynchronously in the background of your application. This can be used to fetch data periodically, do some calculations in the background, or perform some cleanups. This can also be used to send preconfigured emails or whatever you need to do in the background.

HostedServices are basically simple classes, which implements the IHostedService interface.

public class SampleHostedService: IHostedService {
    public Task StartAsync(CancellationToken cancellationToken) {}

    public Task StopAsync(CancellationToken cancellationToken) {}

A HostedService needs tom implement a StartAsync() and a StopAsync() method. The StartAsync() is the place where you implement the logic to execute. This method gets executed once immediately after the application starts. The method StopAsync(),on the other hand, gets executed just before the application stops. This also means that to start a kind of scheduled service you need to implement it on your own. You will need to implement a loop which executes the code regularly.

To get a HostedService executed you need to register it in the ASP.NET Core dependency injection container as a singleton instance:

services.AddSingleton<IHostedService, SampleHostedService>(); 

To see how a hosted service works, I created the next snippet. It writes a log message on start, on stop, and every two seconds to the console:

public class SampleHostedService: IHostedService {
    private readonly ILogger < SampleHostedService > logger;

    // inject a logger
    public SampleHostedService(ILogger < SampleHostedService > logger) {
        this.logger = logger;

    public Task StartAsync(CancellationToken cancellationToken) {
        logger.LogInformation("Hosted service starting");

        return Task.Factory.StartNew(async () => {
            // loop until a cancalation is requested
            while (!cancellationToken.IsCancellationRequested) {
                logger.LogInformation("Hosted service executing - {0}", DateTime.Now);
                try {
                    // wait for 3 seconds
                    await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
                } catch (OperationCanceledException) {}
        }, cancellationToken);

    public Task StopAsync(CancellationToken cancellationToken) {
        logger.LogInformation("Hosted service stopping");
        return Task.CompletedTask;

To test this, I simply created a new ASP.NET Core application, placed this snippet inside, register the HostedService and started the application by calling the next command in the console:

dotnet run

This results in the following console output:

As you can see, the log output is written to the console every two seconds.


You can now start to do some more complex things with HostedServices. Be careful with the hosted service, because it runs all in the same application. Don't use too much CPU or memory, this could slow down your application.

For bigger applications, I would suggest moving such tasks to a separate application that is specialized to execute background tasks. A separate Docker container, a BackroundWorker on Azure, Azure Functions, or something like this. However, it should be separated from the main application in that case.

In the next part of this series, I'm going to write about MiddleWares and how you can use them to implement special logic to the request pipeline, or how you are able to serve specific logic on different paths.

web dev ,asp.net core ,web application development ,tutorial ,hostedservices

Published at DZone with permission of Juergen Gutsch , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}