Running Background tasks using IHostedService and BackgroundService (In ASP.NET Core 3.1)
DotNet Core Central DotNet Core Central
26.1K subscribers
47,710 views
0

 Published On Jul 26, 2020

#dotnetcore #aspnetcore #backgroundservices

Running background tasks in an ASP.NET Core application is often a necessity. Situations where we connect with cloud-managed services like streams and queues. They usually are implemented through continuously running methods. Because most of these managed cloud services are accessed via an HTTP connection, hence there are usually no callbacks.

In this video, I will walk through how to implement running background tasks using the following two methods:
1. Creating a class which implements IHostedService
2. And then creating a class which is derived from BackgroundService

In both cases, we will use the ConfigureServices extension method on the IHostBuilder instance for configuring the hosted service. Although the configuration of the background services can be done inside of the Startup class as well. And I am going to show both the variation in this video.

The hosted background services implementation is deeply integrated with the dependency injection of ASP.NET Core. That makes it very easy to implement the login into a testable component, whereas the hosted services are used only for invoking the components. I am going to demonstrate this as well in this video.

The source code for the video is available in GitHub here: https://github.com/choudhurynirjhar/b...

show more

Share/Embed