Running Background tasks using IHostedService and BackgroundService (In ASP.NET Core 3.1)

preview_player
Показать описание
#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.

Рекомендации по теме
Комментарии
Автор

How do we deploy this on IIS and test whether background service is running without failure?

smrutiranipatro
Автор

When you have such a task running in the background, is there any straightforward way for it to communicate its status so that external processes (eg the one that spawned it) can access that status?

JeffMTX
Автор

This background task is something which is running at regular interval but how can we schedule multiple jobs and pause/resume them if needed with their own independent frequency/interval and monitor them through a UI?

microtech
Автор

Hi, great content.
Is it possible to run BackgroundService on demand ?
Lets say, every API call I want to trigger the backgroundservice.

khertgeverola
Автор

Excellent explanation man!! Thanks a lot!! 🎉🎉👏👏🚀

ManiasInc
Автор

Hi my scheduler automatic application is shutting down... Any time please help me

MithunKiet
Автор

Hi sir, I have a requirement like one process get executed on every 1st day of month . how to achieve this . please reply.

ashutoshmishra
Автор

Great content!
So what is the difference between IhostedService vs BackgroundService approach
Realtime example of When should use which one.

codewithkashif
Автор

is there a way to manually start background job from an Action method instead of at starttime ?

aj.arunkumar
Автор

It's better if you would have added the implementation of stopping the Background Service.
By the way, good job.

saarcfashion
Автор

Great! Clearly talk about how the backgroundservice work with Controller, thx a lot~

MineFreeman
Автор

Hi, I've a scenario. Suppose we have created one job that completes execution within 20 minutes and runs on a time interval of 10 minutes, but if job takes more time (10 minutes more) in execution, then time interval should adjust automatically.

Ex - Suppose a job starts at 10:00AM and completes execution at 10:20AM. Again job will start @10:30AM with the interval of 10 minutes. If Job takes 10 more minutes in execution and completes execution @10:30AM, then again job should start @10:40AM rather than @10:30AM.
Interval time should be increased according to time taken in execution (It should be changed dynamically.)

vineettomar
Автор

thank you. What is the best option to select for you?

sornt
Автор

but where is code to make it reapeat after specific intervals.
i want it to be a scheduled job.
task.delay is just to create a delay after performing dowork. but no code to make it scehduled job.
please help

c-sharptricks
Автор

Important to note that adding the hosted service for method 1 does not work when adding it from startup configureServices, and requires to be added through program createHostBuilder.

illuminaticat
Автор

Good work, now I can write PHP task using Cron Tab in C#, how technology is moving.

parko
Автор

Why you did not created worker service instead of creating a API project?

anantjaisawal
Автор

is it like fire and forget and do we need to do pooling to find whether the task is complete or not, please let me know if there is a way to know the job done to notify user

rajkumar-oyni
Автор

Amazing explanation and very help full video.

shipravats
Автор

but in my case job is not repeating
its being performed only once,
anyone please help

c-sharptricks