No video

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

  Рет қаралды 48,162

DotNet Core Central

DotNet Core Central

Күн бұрын

#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: github.com/cho...

Пікірлер: 101
@MineFreeman
@MineFreeman 3 жыл бұрын
Great! Clearly talk about how the backgroundservice work with Controller, thx a lot~
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Mine Freeman, thanks for watching!
@shipravats536
@shipravats536 Жыл бұрын
Amazing explanation and very help full video.
@DotNetCoreCentral
@DotNetCoreCentral Жыл бұрын
Glad it was helpful!
@ManiasInc
@ManiasInc 2 жыл бұрын
Excellent explanation man!! Thanks a lot!! 🎉🎉👏👏🚀
@MihaiMoisei
@MihaiMoisei 4 жыл бұрын
Thank you for the clear presentation.
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
Mihai Moisei thanks for watching!
@microtech2448
@microtech2448 2 жыл бұрын
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?
@vineettomar4823
@vineettomar4823 2 жыл бұрын
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.)
@DeepWorksStudios
@DeepWorksStudios Жыл бұрын
Did you figured it out? If not i offer you my help :)
@JeffMTX
@JeffMTX 3 жыл бұрын
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?
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@J M, there are a couple of ways to achieve it. 1. is to create an in-memory queue and communicate through it. 2. is to create an event exchange and communicate through that. One thing to keep in mind is that, since they are in different threads, the code needs to be thread-safe.
@noelbrantley3400
@noelbrantley3400 3 жыл бұрын
i know it is kinda randomly asking but do anybody know a good place to watch new movies online ?
@davidemmanuel3001
@davidemmanuel3001 Жыл бұрын
Great video
@minchaudhary
@minchaudhary 3 жыл бұрын
Thank you, you made me clear about background service
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Min Chaudhary, thanks for watching!
@khertgeverola175
@khertgeverola175 2 жыл бұрын
Hi, great content. Is it possible to run BackgroundService on demand ? Lets say, every API call I want to trigger the backgroundservice.
@ukaigwechidi6563
@ukaigwechidi6563 2 жыл бұрын
@DotNet Core Central, please kindly leave a reply to this question above ... @Geverola, I would appreciate you sharing the answer to this if you have got one ...
@ost7487
@ost7487 3 жыл бұрын
Straight to the point. Thanks.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Saitama, thanks for watching!
@rvrby2541
@rvrby2541 3 жыл бұрын
Very useful, thank you very much!
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
RvR By thanks for watching!
@smrutiranipatro102
@smrutiranipatro102 2 жыл бұрын
How do we deploy this on IIS and test whether background service is running without failure?
@merttatlsetsis3350
@merttatlsetsis3350 2 жыл бұрын
Hi, did you find anything about hosting service to IIS
@StefanoLabate
@StefanoLabate 4 жыл бұрын
very useful, thank you!
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
Stefano Labate thanks for watching!
@PrabinYovan
@PrabinYovan 3 жыл бұрын
Awesome video, thank you so much
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
Prabin Yovan thanks for watching!
@shahjahan12003
@shahjahan12003 3 жыл бұрын
Thanks a lot. Very much helpful.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Mohammad Shahjahan Mia, thanks for watching!
@codewithkashif
@codewithkashif 3 жыл бұрын
Great content! So what is the difference between IhostedService vs BackgroundService approach Realtime example of When should use which one.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Kashif Reza, if you need control over how to start and stop your service, you go with IHostedService, otherwise, BackgroundService will be the better approach. Having said that, I personally prefer IHostedService, just my personal preference.
@toetoeag
@toetoeag 3 жыл бұрын
Thank you. Good explanation.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@ToeToe Aung, thanks for watching!
@aj.arunkumar
@aj.arunkumar Жыл бұрын
is there a way to manually start background job from an Action method instead of at starttime ?
@DotNetCoreCentral
@DotNetCoreCentral Жыл бұрын
Not that I know of.
@saarcfashion1196
@saarcfashion1196 2 жыл бұрын
It's better if you would have added the implementation of stopping the Background Service. By the way, good job.
@illuminaticat4861
@illuminaticat4861 3 жыл бұрын
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.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@IlluminatiCat, I am not sure I understand, can you refer to the minute in the video where you see the issue is?
@ArkopalBhattacharya
@ArkopalBhattacharya 3 жыл бұрын
very well explained!
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Arkopal Bhattacharya, thanks for watching!
@ashutoshmishra2170
@ashutoshmishra2170 3 жыл бұрын
Hi sir, I have a requirement like one process get executed on every 1st day of month . how to achieve this . please reply.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Ashutosh Mishra , for that requirement, you should use Hangfire. I have a video here: kzfaq.info/get/bejne/qbepjJOp27CYXaM.html
@schweppesmandarin8839
@schweppesmandarin8839 2 жыл бұрын
Amazing tutorial!
@DotNetCoreCentral
@DotNetCoreCentral 2 жыл бұрын
@Schweppes Mandarin, thanks!
@schweppesmandarin8839
@schweppesmandarin8839 2 жыл бұрын
@@DotNetCoreCentral I sent you an email but I did not receive a response yet
@aargomemnon
@aargomemnon 3 жыл бұрын
Thanks for this video. I learned a lot. Also is it possible to manually start and stop this service with UI?
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Gama Sharma, thanks for watching! Yes, you can do it, but you will need to expose this through an API.
@microtech2448
@microtech2448 2 жыл бұрын
@@DotNetCoreCentral can you please give example of this? Also, can we create dashboard or persist ongoing tasks in database and process them and see their status updated over time just like in hangfire?
@musandlala7991
@musandlala7991 3 жыл бұрын
Lovely work. I am wondering how safe would this be for running automated Entity Framework calls
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Kmn I, thanks for watching! With respect to running background jobs to access EF calls, I do not see any reason why it would be unsafe. Unless the query ran can cause potential deadlocks, in which case anyway the design has to be rethought. Let me know if it makes sense. Thanks
@sornt
@sornt 4 жыл бұрын
thank you. What is the best option to select for you?
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
Sorn Toomgasorn thanks for watching! I prefer using first option of interface as interface always give you more flexibility.
@sornt
@sornt 4 жыл бұрын
DotNet Core Central thank you for your reply.
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
Sorn Toomgasorn you are welcome!
@mscotthowell1
@mscotthowell1 4 жыл бұрын
Great work.
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
@Scott Howell thanks!
@c-sharptricks9899
@c-sharptricks9899 3 жыл бұрын
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
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@C-Sharp Tricks , if you want scheduled jobs, the better option is to use Hangfire. I have a video here: kzfaq.info/get/bejne/qbepjJOp27CYXaM.html
@bilalelmursi4039
@bilalelmursi4039 3 жыл бұрын
thank you!
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Bilal Elmursi, thanks for watching!
@rajkumar-oy3ni
@rajkumar-oy3ni 3 жыл бұрын
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
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@raj kumar, on job complete you can fire a SignalR message to notify the user.
@sleepyinvestorcoder
@sleepyinvestorcoder 3 жыл бұрын
Hi, such a great video! if we close the browser the service will stop? if yes. is there a way that even the browser closes the service will continue?
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@noriel ablana, if you are debugging locally yes it will. But not when deployed in dev/qa/prod. Also, you can configure your Visual Studio not to stop the process on browser close.
@sleepyinvestorcoder
@sleepyinvestorcoder 3 жыл бұрын
@@DotNetCoreCentral I was just testing (in development) and i just thought that it is running even the browser closes, luckily you mentioned here about production, can you teach me how to do that in visual studio? thanks again!
@rajkumar-oy3ni
@rajkumar-oy3ni 3 жыл бұрын
Thank you for the video, but here how do we return response to UI once completed
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@raj kumar, if you want a response to a background job to be returned to the UI, there are multiple ways of doing it. SignalR is one of them.
@MithunKiet
@MithunKiet 2 жыл бұрын
Hi my scheduler automatic application is shutting down... Any time please help me
@DotNetCoreCentral
@DotNetCoreCentral 2 жыл бұрын
@Mithun Kumar, where are you running the application?
@parko1965
@parko1965 3 жыл бұрын
Good work, now I can write PHP task using Cron Tab in C#, how technology is moving.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Davey Lad, thanks for watching!
@Akustik
@Akustik 3 жыл бұрын
Very useful and well explained. One thing I'm unclear on, why do you AddHostedService() inside Program.cs, rather than Startup.cs? As far as I can tell the method ConfigureServices() is the same.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Akustik, you can use it in Startup, I use it there as I feel since it's a service on its own and running in its own thread, makes it clearer if it is started from the Program class. Again, there is no real technical reason, just my personal coding preference. Hope this answers your question. And thanks for watching!
@chad9861
@chad9861 2 жыл бұрын
Great video!! Very precise and easy to understand. Thanks for putting such a short video covering bs😃! Just heads up - Going forward in .net6, you can write whole initialization code in classless main with implied using namespace. It also allows old style of using main/startup combo
@anantjaisawal8455
@anantjaisawal8455 3 жыл бұрын
Why you did not created worker service instead of creating a API project?
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Anant Jaisawal, the main idea is running the background task part of a service, hence the API Project.
@aymanmetwally5560
@aymanmetwally5560 4 жыл бұрын
Great
@DotNetCoreCentral
@DotNetCoreCentral 4 жыл бұрын
@ayman metwally thanks for watching!
@nguyenvantrinh4924
@nguyenvantrinh4924 Жыл бұрын
thank bro
@DotNetCoreCentral
@DotNetCoreCentral Жыл бұрын
Thanks for watching!
@c-sharptricks9899
@c-sharptricks9899 3 жыл бұрын
but in my case job is not repeating its being performed only once, anyone please help
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@C-Sharp Tricks, can you please share your code in github or somewhere so I can take a look? Thanks
@ashwinikumarshende4777
@ashwinikumarshende4777 3 жыл бұрын
Where can i get whole code?
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Ashwinikumar Shende, you can get the source code in my GitHub repo here: github.com/choudhurynirjhar/backgroundtask-demo
@microtech2448
@microtech2448 2 жыл бұрын
Will this work with IIS idle timeout scenario?
@DotNetCoreCentral
@DotNetCoreCentral 2 жыл бұрын
@Micro Tech, I have not been using IIS/Windows for more than a few years, all of my services are now in Docker/Linux. Having said that, I know IIS shuts down appdomain if no external HTTP activity. And for that scenario, the only option is to have a periodic health check or ping to keep the server alive. I know it's not an ideal solution, but as far as I know, this is the solution that might work.
@microtech2448
@microtech2448 2 жыл бұрын
@@DotNetCoreCentral so how do you keep background services (which are hosted with .net core api/mvc) all the time active in docker/linux? Doesn't it stop afyer certain time of inactivity like iis? Thanks!
@DotNetCoreCentral
@DotNetCoreCentral 2 жыл бұрын
@@microtech2448 for .NET Core services running in Linux/Docker hosted in AWS they are always pinged for health check by ECS which keeps it running irrespective.
@microtech2448
@microtech2448 2 жыл бұрын
@@DotNetCoreCentral thanks for the explanation..
@DotNetCoreCentral
@DotNetCoreCentral 2 жыл бұрын
@@microtech2448 welcome!
@ersoyodabas2251
@ersoyodabas2251 3 жыл бұрын
thank u
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Ersoy Odabas, thanks for watching!
@parko1965
@parko1965 3 жыл бұрын
Background service is killed when api receives no requests after certain amount of time. Not good.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@Davey Lad, this should not be the case, if you can share your code I can take a look into what is going on there.
@parko1965
@parko1965 3 жыл бұрын
@@DotNetCoreCentral I'm publishing to a live webserver and as soon as the app pool's "idle timeout" property is fulfilled the background service is killed.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@@parko1965 To be honest I have not been running .NET Core in Windows and IIS for over a year now in Test or Production. I have been using out of the box kestrel web server running in a docker container for Production, and locally using self-hosted in the console. And I have never faced any issue. I think in your case, IIS is shutting down the app. I can try it out on my side and see what can be done with IIS.
@parko1965
@parko1965 3 жыл бұрын
@@DotNetCoreCentral I have a shared hosting solution and modification of the app pool time out is not an option. I do have a work around but it's not ideal.
@DotNetCoreCentral
@DotNetCoreCentral 3 жыл бұрын
@@parko1965 I will take a look and see what is the issue with IIS/app pool time out.
How to use Middleware in ASP.NET Core (2 ways of implementing middleware)
17:59
Lehanga 🤣 #comedy #funny
00:31
Micky Makeover
Рет қаралды 28 МЛН
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 92 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 48 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 22 МЛН
An introduction to MassTransit using RabbitMQ (In ASP.NET Core)
22:11
DotNet Core Central
Рет қаралды 46 М.
Building Reactive Microservice with RabbitMQ and ASP.NET Core
31:58
DotNet Core Central
Рет қаралды 35 М.
Scheduling recurring jobs with Hangfire (In ASP.Net Core 3.1)
14:16
DotNet Core Central
Рет қаралды 53 М.
Background Tasks Are Finally Fixed in .NET 8
10:29
Nick Chapsas
Рет қаралды 106 М.
Hangfire - Hangfire in .Net Core | Hangfire Asp.Net Core
16:17
Coding Droplets
Рет қаралды 22 М.
How To Build Loosely Coupled Microservices With MassTransit
23:01
Milan Jovanović
Рет қаралды 32 М.
Lehanga 🤣 #comedy #funny
00:31
Micky Makeover
Рет қаралды 28 МЛН