Тёмный

Background Jobs in ASP.NET Core 

IAmTimCorey
Подписаться 421 тыс.
Просмотров 53 тыс.
50% 1

Опубликовано:

 

25 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 139   
@ayarasimeon5592
@ayarasimeon5592 6 месяцев назад
Thanks for all you do Tim, you are a blessing to C# developers and software engineers in general
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
I’m glad my content has been so helpful.
@nosaanthony7310
@nosaanthony7310 6 месяцев назад
I always love your approach and simplicity. My friends are tired of me because of how I advocate for your contents 😂
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks for sharing!
@webluke
@webluke 6 месяцев назад
That's super handy to have built right into ASP.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
It really is.
@MohdRizwan-pf3ex
@MohdRizwan-pf3ex 6 месяцев назад
"You are awesome. I am highly grateful to you. I am a developer because of you. Thank you so much for your tremendous work."
@rty361
@rty361 6 месяцев назад
bool isLyingInTheAboveComment = false;
@shewartshepherd1126
@shewartshepherd1126 6 месяцев назад
@@rty361 that's a great one hahaha!
@MohdRizwan-pf3ex
@MohdRizwan-pf3ex 6 месяцев назад
@@rty361public class DeveloperLife { public bool GetsGoodPackage { get; set; } public bool WorksAtReputedCompany { get; set; } public bool KeepsLearning { get; set; } // Constructor sets all the properties to true by default. public DeveloperLife() { GetsGoodPackage = true; WorksAtReputedCompany = true; KeepsLearning = true; } // Method to check if the developer is lying about their success. public bool IsLyingInTheAboveComment() { // If all conditions are true, then not lying; otherwise, lying. return !(GetsGoodPackage && WorksAtReputedCompany && KeepsLearning); } }
@rty361
@rty361 6 месяцев назад
@@MohdRizwan-pf3ex I love this😂
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You're very welcome!
@VitorCosta-pb7hx
@VitorCosta-pb7hx 4 месяца назад
Most excellent simple and concise tutorial. Thanks
@IAmTimCorey
@IAmTimCorey 4 месяца назад
You're welcome!
@bmiller949
@bmiller949 6 месяцев назад
I like ingesting these skills in small bites like this.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
I'm glad.
@clebersondot-net
@clebersondot-net 6 месяцев назад
Thank you, Tim! It's what i've been looking for
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@Pro-sj1kh
@Pro-sj1kh 2 месяца назад
the best never rest thank you Corey
@IAmTimCorey
@IAmTimCorey 2 месяца назад
You are welcome.
@lyudmilpetrov9461
@lyudmilpetrov9461 6 месяцев назад
Thank you so much for knowledge sharing and God bless you and your family
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@rajkumararora7366
@rajkumararora7366 6 месяцев назад
Great work. Thanks for sharing!
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@АлексейОрёл-к3у
@АлексейОрёл-к3у 6 месяцев назад
If anyone is using Timer, I want to warn you that when deploying an application on linux the Timer class will not work correctly, Timer will only run once. It took me a long time to find this bug!!!
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks for sharing!
@awmy3109
@awmy3109 6 месяцев назад
Why is that the case? Are you sure you don't have system time configuration issues?
@Joooooooooooosh
@Joooooooooooosh 6 месяцев назад
​@@awmy3109Yeah I'm not buying it. That would be a pretty glaring, show stopping bug.
@АлексейОрёл-к3у
@АлексейОрёл-к3у 6 месяцев назад
nope@@awmy3109
@akshayrajgollahalli
@akshayrajgollahalli 6 месяцев назад
Is there a bug ticket for this?
@10Totti
@10Totti 6 месяцев назад
Nice tutorial thanks!
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@lborate3543
@lborate3543 6 месяцев назад
Tim, I have a video idea. I'm training a new hire, and they asked me a good question I think other could benefit from. The long and short of it is F12. How to look into a class to see the available methods, and how to look at what the returns look like i.e. node, and the overload options without doing the hover and up down arrows when you hover over a method. And I've always wondered why there is a .ToString(), but there isn't a .ToInt32(). Why do I have to use Convert.ToInt32... go
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@Joooooooooooosh
@Joooooooooooosh 6 месяцев назад
Because everything can be converted to a string, whereas only certain values can be converted to Int32. Doesn't make sense as an instance method.
@lborate3543
@lborate3543 6 месяцев назад
@JoshEinstein you've never gotten a string exception? Not everything can go ToString. If you can get convert errors, and you can get string exceptions, I don't understand your comment. No disrespect.
@Joooooooooooosh
@Joooooooooooosh 6 месяцев назад
@@lborate3543 If you are getting an exception from a ToString call, it means it's written wrong. ToString is by design never supposed to throw.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
To follow up here, everything derives from object. Object includes a ToString() method. Everything should be able to output a valid string. If it does not, some developer made a bad mistake.
@SagharMax
@SagharMax 6 месяцев назад
It is perfect and helpful. Thank you for this.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@VishalTrivediB
@VishalTrivediB 27 дней назад
Excellent video as always, thanks Tim! Question: What is the best way, in your opinion, to trigger a user initiated background task, for e.g. from a web page?
@IAmTimCorey
@IAmTimCorey 24 дня назад
I think an easy way is to have the user add something to the database table that acts as a queue. Then the background task can pick it up and process it when it has time.
@VishalTrivediB
@VishalTrivediB 23 дня назад
@@IAmTimCorey Thank you, that's a great tip.
@andyhb1970
@andyhb1970 6 месяцев назад
I assume with any background job technology Hangfire, Quartz, Hosted Service etc. if the IIS process isnt running then neither is your job. I used Quartz fairly recently and the job just wasnt running until i visited a page to trigger the process. Ultimate ly I abandoned it and refactored everything into a Windows Service.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
That’s just a setting in IIS. Change it and your site will continue to run.
@StanleyUkweze
@StanleyUkweze 6 месяцев назад
Thank you Tim
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@AlexanderBelikov
@AlexanderBelikov 6 месяцев назад
The issue I faced was the app pool will be stopped if there were no incomming requests for some time. The background tasks will also stop in this case. I tried to fix it on a app pool settings level but gave up and hosted my background workers under win service. Is there a good way to keep the app always on?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Yes, there is. I’ll do a video on it when I can.
@carlos.maradiaga
@carlos.maradiaga 6 месяцев назад
Excellent. I will use this approach to return the amount sent by the user from their account to another destination account at another bank or financial institution and the transaction failed for whatever reason. Therefore, the process of returning the amount to the original account will be automatic, since there will be a process running in the background verifying the result of the transfer.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Great!
@louiseeggleton7420
@louiseeggleton7420 6 месяцев назад
I'd probably use something like Hangfire for that since there is tracking and failover.
@AlexxXRecorD
@AlexxXRecorD 5 месяцев назад
Nice, thanks so much! Very interesting.
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
You are welcome.
@sigma561
@sigma561 6 месяцев назад
Great, what is the extension are you using to implement suggestions please?
@MrFalcon58199
@MrFalcon58199 6 месяцев назад
Is there any significant difference between creating a background job like this vs. adding a Worker Service to the solution besides not having to create a separate project?
@DoYourForm
@DoYourForm 6 месяцев назад
Timer uses separate thread, so it breaks DI container and EF will not work (can't use already existing context). No way to use the video to access the database, agree with @AndreSilveira1.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
The code inside the background service is to demonstrate how you could use it. You get to choose what code to put in there. You can absolutely access data access from a background service. I even posted a link on how to do that in the other comment thread.
@mohammedelsuissey1745
@mohammedelsuissey1745 6 месяцев назад
I know this is just for training purposes, however there was no need to dispose the timer in a dispose method, you could just dispose it in the stop method as you create a new instance in the start method. Great material though, thanks.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
There actually is a need. If the application crashes, the stop won't be called but the dispose will be called. This way, it gets properly closed down no matter what.
@mohammedelsuissey1745
@mohammedelsuissey1745 6 месяцев назад
@@IAmTimCorey Good peice of knowledge here, thanks.
@ТимурДегтярев-х3к
@ТимурДегтярев-х3к 4 месяца назад
Dear Tim, I have a question regarding hosted services. What if I need the service run indefinetely in the background (i.e. receiving some data via TCP) but I need to be able to change some fields in the data saved like add timestamp or some additional info? Should I use hosted service for TCP reader as well?
@phoenixishere
@phoenixishere 6 месяцев назад
can we just trigger this service manually just like we do for an azure function? Say i want to enqueue a job every time a logic runs ! , i can write the logic in IHostedService implementation but triggering it manually though !
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Wouldn’t that just be an API endpoint? Or a method? I don’t think you need a service for that.
@rospy7
@rospy7 6 месяцев назад
if you want to trigger a background service from the UI, then you should provide an endpoint to the user that creates some kind of "initiation request" record that is persisted in a database or a thread safe repository (memory cache, concurrent bag as in the example, disk, etc). Then your background service would check periodically among the collection of requests for the ones that haven't been picked already picked up and execute them. If you also want to report the progress of the job to the end user, you would provide a second endpoint that polls for the job with the id that is returned from your first method. At least that's one way to accomplish this
@phoenixishere
@phoenixishere 6 месяцев назад
@@IAmTimCorey well what if its a long running process which may exceeds gateway timeout.... i can have 2 endpoints 1 to trigger the background job and one to poll the status of the job
@leknyzma
@leknyzma 6 месяцев назад
why are you not using PeriodicTimer ?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Timer is easy, quick, and works for the situation. The specific code you use will depend on your scenario.
@dineshyd
@dineshyd 2 месяца назад
good explanation
@IAmTimCorey
@IAmTimCorey 2 месяца назад
Thank you!
@davidmontilla6466
@davidmontilla6466 3 месяца назад
Great video Tim , but when you deploy a web api with a background service, this service it will not start until some endpoint on the api is hit, is this normal behavior for hostedServices?
@IAmTimCorey
@IAmTimCorey 3 месяца назад
An API (or any website, really) should be accessed right after deployment to initialize everything. Otherwise, it will conserve resources and wait for the first caller to set things up. It can take a while for that first call to finish processing everything. Instead, run it right after deployment to get everything warmed up. This will also kick off your background job.
@jan5310
@jan5310 5 месяцев назад
I can highly recommend Quartz
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
Thanks for sharing!
@pricenetSA
@pricenetSA 6 месяцев назад
What will happen if there are no active sessions for a period of time in IIS? There would be no instances running. How would the background task run? I tried using Coravel a while ago to run scheduled tasks. It stopped running everytime there were no active instances.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
This is an IIS configuration setting. You can have it keep the website live all the time. But yes, if you have IIS set up to conserve resources when they aren't used, your site will be turned off when not used.
@danielgochin3703
@danielgochin3703 6 месяцев назад
@@IAmTimCorey Thanks for the explanation!
@andyhb1970
@andyhb1970 6 месяцев назад
Or like me I was using Azure App Service and had no control at the service level I was targeting
@vamvdotnet
@vamvdotnet 6 месяцев назад
Would the PeriodicTimer class work great for this use case as well?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Yep.
@null-qk8si
@null-qk8si 6 месяцев назад
what if i want to run a single task in the back ground when an specific Api is called i tried Task.Run(()=>task(data)) inside the api but it breaks when using an ef query inside task(data)
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You need to make sure that you are matching the dependency lifetime values (singletons cannot directly consume scoped services like EF). There is a commend under this video that asks about it and I explain how it works and how you can use EF in a background service.
@null-qk8si
@null-qk8si 6 месяцев назад
@@IAmTimCorey thnx for the replay I used Services.AddDbContextFactory in the program file this made my last Fire and Forget code works dose using _contextFactory.CreateDbContext(); have an drawbacks ?
@loveandlike6747
@loveandlike6747 6 месяцев назад
Thanks , I have a question I’m just graduated from a boot camp with C sharp css html JavaScript but I don’t know how to create projects, if you please give me recommendations or guidance, I would really appreciate it ?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
My recommendation is to build small things. You need LOTS of practice actually building what you learn. Here is a video explaining more: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-viigJ9NwJ2o.htmlsi=CxLVka7hnAd-wkYi
@miguelynancy10
@miguelynancy10 Месяц назад
Hi, thanks for the video, I was implementing a background service with IHostedService to send emails but it is sending 3 the email times when I deploy it to a webapp in azure it is because the webapp has 3 instance is there a way to send it only 1?
@IAmTimCorey
@IAmTimCorey Месяц назад
Yes, if you run three instances of the background service and don't account for that in your code, you will have this issue. Instead, what you need to do is have a locking mechanism in your code so that only one instance handles any one request. Otherwise, you aren't actually spreading out the work, you are just duplicating the work. There is no need to have three instances if they are all doing the same thing. You need to write the code so that each does a different set of work.
@anandshukla5071
@anandshukla5071 6 месяцев назад
Timer doesn't wait for the existing task to finish before starting another task right....so if we are making any db work and the work we are doing is taking time and mean while timer starts another same task so kind of locking database right
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Correct. If you are going to have jobs that last longer than your interval, and that is a problem, either extend your interval or turn off the timer while the job runs and then start it when the job is over.
@leroyvan7357
@leroyvan7357 6 месяцев назад
thanks you
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@GaryJohnWalker1
@GaryJohnWalker1 6 месяцев назад
Maybe cover Hangfire (and then Quartz!) in the nearish future?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks for the suggestion. You can post it on suggestions.iamtimcorey.com so that others can upvote it.
@milemihailov1
@milemihailov1 6 месяцев назад
Hi Tim, great video. I have a question is it possible to do the same on Blazor Server App?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Yes, you can.
@andywalter7426
@andywalter7426 6 месяцев назад
One question I have is I noticed when you used idisposable, there is always a weird warning that i saw you ignored. I always have it supressed. Do you know what can be done so a person can do IDisposable without being forced to use the IDisposable pattern.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
What "weird warning"? Can you give me a time code and the message? As for your last sentence, I'm not sure what that means. Do you not want to use the interface but want the benefits of the interface? If so then no, that won't work.
@andywalter7426
@andywalter7426 6 месяцев назад
@@IAmTimCorey If you mouse over the dispose method it gives the message saying ""Change {class{}.Dispose() to call GC.SuppressFinalize(object). This will prevent derived types that introduce a finalizer from needing to re-implement 'IDisposable' to call it. I was hoping to have a way so that warning won't keep happening everytime you implement the IDisposable. The only case that does not happen is if you use the option that autogenerates the code for following the IDisposable pattern.
@gowthamkarukola2803
@gowthamkarukola2803 6 месяцев назад
very good
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks!
@S3Kglitches
@S3Kglitches 6 месяцев назад
How to in MAUI? Need reliable background GPS scanning.
6 месяцев назад
Hi, I tried to implement it on a MVC project, but I need to inject DB Context and when I do so, it returns a error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: MyProj.Services.MyBackgroundThing': Cannot consume scoped service 'MyProj.Data.ApplicationDbContext' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.)' How do I fix that?
6 месяцев назад
Well, if anyone have the same problem, that's how I solved it: inected private readonly IServiceScopeFactory _scopeFactory; on the constructor On the method: using (var scope = _scopeFactory.CreateScope()){ var someList = scope.ServiceProvider.GetRequiredService(); // call whatever you want SINCHRONESLY. // ASYNC methods won't work. } If anyone have a better solution, please let me know. This stuff is quite usefull!!
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You need to match type lifetimes. So your background service is a singleton. A singleton cannot consume a scoped service, because that will expose one scope to everyone. That's a function of how dependency injection works.
6 месяцев назад
@@IAmTimCorey Thank you for your response, but how do I fix this? Can't I access database from my background services? I solved by inectinig IServiceScopeFactory into my background service class, but it doesn't allow async methods when I call my DbContext... is there a better solution?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Here is some direction on how to do it well: learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service
@joebananas10
@joebananas10 6 месяцев назад
Would this approach be good for maintaining data in combo-boxes? e.g. Imagine an application that has a bunch of combo-boxes on a page and instead of each user's instances having to hit the database every-time the page is loaded, could this be used to maintain cached versions of those lists, which are updated regularly or on demand regardless of the user landing on the page. Or is there a better way to accomplish that type of updating?
@MuhammadYousaf-wp6wj
@MuhammadYousaf-wp6wj 6 месяцев назад
I am facing similar situation like you.. waiting for the reply.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
This runs server-side, but yes. You could ping for new data often, and the system could always pull from the cache (which is refreshed on a schedule). That way, the database only gets hit on cache refresh, regardless of how many times the clients call for new data. However, there is still going to be calls from the client to the server in this model. If you want this to happen inside the page in the background, you would need to write something to refresh dropdowns based upon a client-side timer.
@joebananas10
@joebananas10 6 месяцев назад
@@IAmTimCorey thanks for that. i'm maintaining an older app written by other developers that has exactly this, a page with a bunch of combo-boxes and every time the user lands on the page there's a separate DB hit for every combo-box. The data in the CB's is rarely updated so the lists are effectively static. i might rather have the CBs filled with a cache on the server rather than another hit to the DB. then only update the cache when the list maintenance screens are utilized. now that i'm writing this, it may be overly complicated for no real benefit but still, DB hits are expensive in terms of performance.
@cakdham4607
@cakdham4607 6 месяцев назад
How to run hundred background jobs in precision periode of time. Example every 10ms. Thanks.
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You probably want to consider Hangfire.
@bentslattsveen2208
@bentslattsveen2208 6 месяцев назад
Nice video👍 How would you start multiple jobs of the same type, but you don’t know how many before you start and read from a source, config of some sort (database)?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
The background jobs have access to Dependency Injection just like everything else, so if you need to read from the database or a configuration, you can pass in those dependencies and call them.
@bentslattsveen2208
@bentslattsveen2208 6 месяцев назад
@@IAmTimCorey Sorry, my question was not clear. My question wasn’t about if or how to get access to db, but how to dynamically in code create x instances/numbers of background jobs. When you have read the db, you know how many. Something like a bootstrapper
6 месяцев назад
@@IAmTimCorey I did and I had the scope error mentioned above... I couldn't inject dbcontext into the service class. IHostedServices is singleton and DbContext is scopped
@muttBunch
@muttBunch 6 месяцев назад
I need C# rehab. I have a bad habit of caching EVERYTHING! ❤
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
👍
@leroyvan7357
@leroyvan7357 6 месяцев назад
please could you do tutorial on hangfire ?
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@leroyvan7357
@leroyvan7357 6 месяцев назад
@@IAmTimCorey OKay
@HunterShenep
@HunterShenep 3 месяца назад
Your email stuff is broken. Tried to send code to my gmail and icloud. Nothing after a day
@IAmTimCorey
@IAmTimCorey 3 месяца назад
I'm at a conference right now, but I am working on it.
@salimanahmad5352
@salimanahmad5352 5 месяцев назад
Awesome. but how to handle errors
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
The same way you handle errors anywhere else.
@salimanahmad5352
@salimanahmad5352 5 месяцев назад
@@IAmTimCorey thanks
@gowthamkarukola2803
@gowthamkarukola2803 6 месяцев назад
good
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
Thank you!
@leozt99
@leozt99 4 месяца назад
Always those same trivial examples for no use cases
@IAmTimCorey
@IAmTimCorey 4 месяца назад
The point isn't to build a working app for you to just copy and paste. The point is to show you how to use the technology. Then you figure out when it might be useful to you to implement the technology in your application. In this case, though, I also gave you verbally a number of times when you might use this particular technology, such as for refreshing caches, etc.
Далее
Background Tasks Are Finally Fixed in .NET 8
10:29
Просмотров 110 тыс.
Scheduling Background Tasks In .NET With Quartz
10:51
THEY'RE EATING THE DOGS DANCE REMIX!
00:10
Просмотров 193 тыс.
How to create CoreWCF
22:16
Просмотров 378
The Only .NET Scheduler You Should Be Using!
16:38
Просмотров 51 тыс.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Dependency Injection in .NET Core (.NET 6)
1:00:32
Просмотров 190 тыс.