How to Work With Hangfire in ASP.NET Core 7.0|| How to Implement Hangfire in ASP.NET Core 7.0 Jobs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is mro code and welcome back to our today's video so if you're new to this channel consider subscribing and watching our video so today we are going to learn about hangfire with asp.net Co 7.0 and majorly we are going to deal with background jobs so in today's video we are going to learn details about hangfire with sp. net C 7.0 background jobs so almost all application require some kind of background work so whether it's a long or a short work or a repeated job then we have to do every other day so something that we need to see on today's video is how to implement these background jobs in sp. netco so running jobs in the background is a common programming task that we face on regular basis and Performing these jobs correctly without breaking your code isn't easy but it's also not difficult so I used to use window services to schedule different operations Within my C program so then I discovered a hangfire so in today's video we are going to see how you can actually execute these background jobs using hangfire so let's begin the video what is hangfire some of you may might be wondering what is hangfire so hangfire is an open source and well documented task a schedul for a speedon net co uh it's multi- threaded easily scalable and offs a variety of job job type so it is well structured simple to use and gives a performance uh a powerful performance so so without taking much information let's just begin and create a project so I'm going to create a project so I'm going to create isp. net called web app so just uh web app so choose sp. net Co web app MVC so that is model view controller then click next so I'm going to say anire so we can call this sample anire project so this is our project so for those who are new to this channel consider subscribing and watching uh more about our videos so simple hangfire project then next then we are going to use sp. netco 7.0 then uh configure htps then create a new so and there we are so so this is our project so our project has been created we have the controllers the models and the views so if we just launch this app we have a default s.net Co web app so there we are so can you can see this is our default web app so to begin with we are going to need some nuggets to actually install so I'll just showcase so come to our main project right click on the project then manage nuggets then we are going to install hangfire so just type in hangfire so this should be hung fire so that is it so we are going to install hfire there you are accept then another thing that I want us to install I want us to install some uh so SQL so Entity framework Co SQL install that install Entity framework then also install Entity framework called SQL Server so we are going to connect this with our applic with our database and also install Entity framework called SQL client lql client so I think that is fine you can just do that then we can also install uh tools so let's install tools there we are so install that you can also install design so there we are so we have some nuggets so for you to see those nuggets come to our package then you'll be able to see we have the Entity framework code design SQL and the tools and also we have our hangfire there so we need to configure our app settings to connect to our database so to do that so just type in configurations sorry so let's type in configuration so we going to do connection settings it's actually connections so type in connections then I'm going to have here our connection string default connection then you're going to connect this to our server so our database here we have our database so I'm going to create a database called anire sample sample so there you are so this is our database so let's uh take our server server name is this is my server name so I'm going to uh paste it here so I'll just type in in server then I do that then uh database so I need to connect this with the database sorry it should be this way then our database we say it is hangfire sample so this is the database so I'll paste in there then I need to so we can have trust trusted connection so you can say trust trusted connection connection so these will be true so this is true you can also minimize that we can also say multiple multiple active multiple active result sets this is also true typ in true sorry then uh we can also do trusted server certificate this is also true this is also true so this is our connection so the next thing that we should do is actually now connect this to our database so come into our program.cs but before that I need us to create something called application DB context so let's create a folder called database database sorry database then inside the database let's create a class called application DB context DB context then should inherit from DB context and this we should be using Entity framework core that is it then let's create a public application application so this should be application DB context so this will be application DB context then she'll do that DB context options then we should do application DB context then this will be uh options then can do that can do base options so this you be so if you close this then you say base DB context options application DB context then options then we should actually open that up so this should be this way so the next thing that you need to do is come to our come to our program. CS file then we need to have so here we need to come here and say so Builder do Services dot hard anire so you can hard anire then you can actually connect this to our database so you can say x dot use SQL so use SQL Server storage then you can do that we connect it to our connection so Builder dot configuration so you can say configuration do get connection string then here we pass our connection string so our connection string is this one default connection so come to our take that then paste it here then you can close it so that is it so the next thing that you can actually do is uh we need to be accessing some of our background jobs on our dashboard hangfire dashboard so come under the app here so we can actually do here can do something so you can say app do hangfire use hangfire dashboard this is the URL that you should be uh using to access our dashboard so you can say Hang Fire or we say my dashboard my dashboard so this when you put a project slash my dashboard then you should be able to access the hangfire dashboard so let's try to run our project so if we run our project uh something went wrong there keyword not supported so our connection issue so come in here so default connection then server so this is our server then database you should actually do this when connecting to our database should do we should do this then here as well we should be able to do that so server this is it database should be this on trusted connection should be that one then multiple active result sets to trust trusted server CER certificate this should be trust server certificate then let's run it there you are so our project has run so so there you are our project has started so if we come to our program.cs file to access our dashboard we said we we should use my dashboard so let's copy that come to our browser then put in slash my dashboard so if you press enter it will actually take you to the hangfire dashboard so this is the hangfire dashboard so you can see connection and one thing that it has done remember we connected this to our to our SQL so one thing if you come to our database it it has actually created some tables so you can see it has created the hangfire tables the hangfire hash counter job job queue job list so that is what it has done so those are some of the uh hire database schema that it has actually created so hire checks if you have any Associated hire schema available in your database so if not it will actually crate a bunch of tables with you so as you can see that are some of the uh tables for hangfire so so if you come to jobs so if we if we come to our app we should be able to see here we have the jobs so there are no any cued jobs currently so if you also come to r that are there there are no R recurring jobs and the servers so that is what is available so let's try to create something uh V uh let's try to create a background job so let's assume we want to be sending an email uh on our on our application so let's do that so let's create so let's create some a folder here called uh so let's create a folder called interfaces so let's create an an interface class called call iail service so if we press that then you can say this is uh we are now sent we sent email so we want to create a background service so then this will uh take the job type so let's say job type and you can have a start so you can say this is string so this should be start time then let's create another class here let's just add it within here or we can can add it just within the same so we can have this as email service then we will say this actually implements our imail service then here we can say public then void send email then it should take stream job type then string start so this should be start time then here we can actually console this dot right line so it should be job type can say and start time then we can put in something here we [Music] say email success successfully sent then we can put in the exact time so we can do that then we can put this here we say date time then dot now then you can say this to long to Long dates time long time string so that is it so the next thing that we need to do we need to inject this to our program.cs come here then we will say Builder so you'll say Builder dot Services Dot add transient then we put in our IM email service then we say email service so after that I want us to create a controller so let's create a controller here create a new empty controller we call this hangfire controller hire controller anire controller so this is our controller so here you're going to do something here you say public so you can say this is private so let's do private then we do I email service then we say that is email service then we can do private I background job client then we do background job client then you can also do private I recuring job manager sorry so you can do regarding job manager so you do this underscore and you close this then here we need a Constructor so so we we can do public anire [Music] controller then on our anire controller we need High mail so let's tap so you can see IM mail service then we do I background service and current Jo service then email service we have done that so we have created our controller so the next thing that you need to do so background jobs in.net core can be of many types depending on the requirements so let's go through the job types available within anire with proper implementation and explanation on ip.net C project so so let's fire and forget jobs so first fire and forget jobs are executed only once so we have the fire and forget jobs that are executed only once and almost immediately after creation so we will create our first background job so when we create this type of job the server execute it only once as soon as it can so let's create it so we'll do public so we have the fire and forget jobs so public action result set then I'll create fire so this is create fire and forget job so if we do that we don't have to pass any ID so this is uh so this is let's say this HTTP G and uh we can have the root can have the root so this should be root and the root is so we can name it as fire and forget job so fire and forget job so what it does so we have the background job client then we say NQ so we can do NQ then we do that then you say this is email service do send email so remember we we were to send the job type and the start time so the job type here we will say it's fire and forget job then our time it should be date time dat time do now then you can put this to long to long time stream there you are so we have email so this is uh so this should be in [Music] string so you can say to stram yeah that should be that way this will be this way yeah so then we can close this there you are so the next thing is to return so we can return so this we we should return okay so then we that is it so here so so let's assume this is kind of an API so you can have this as control control AB this control there you are so if we do that if we execute these it should actually set fire and forget the job so our only endpoint uses the injector and fire interface to schedule this type of job so we can actually call this uh so if we run this app now let's try to run it and we call this anire so if we come here and we say anire so that is our controller then we put in the the end point so if we hit there and we try to run this let's try we see there you are so it has actually come here so if we do this it has executed and it's okay so it has returned okay so if we do my dashboard my dashboard we can be able to come here and see there is one job so you can see there's one job that is has been queued and if you open it this is what it has happened so we need to see if that is was successful or it is not so you can see it has been cued it has not been uh it is not still yet successful so if we come here and we do that then we say it was created a minute ago and it is cued so let's try and wait to see that let's see so if we do this you can see background job executed successfully if we do my dashboard come to jobs you can see we have our email service activate uh send email and the time so this is what uh you do with email service so that is fire and forget so we have we have another job type so the delayed jobs so for the delayed jobs assuming we want to send an email to a user not immediately but after 10 minutes so in such cases we use delayed jobs so for delayed jobs we'll come here under NQ and we change this to schedule so we change this to schedule then on the schedule we will send our email then here we need to delay this so we'll say time span we delay this to at some for some minutes so we'll say from minutes then we say uh two sorry so we say two minutes so we are delaying our schedule for 2 minutes so then we'll say so what will pass you say background job so you can do this sorry so background job execution can say execution execution delayed for so you can can do that so we say two so you can actually say delayed for two minutes so after we have done that let's try to launch our app so the job will be delayed for a time span of 2 minutes so if we just do forget forget so you can see background job ex delayed for 2 minutes so if we go to my dashboard come here so you'll see there is a scheduled jobs here so this has been scheduled and you can see if we open these jobs if we click here you can see we have enq in 2 minutes so it will be sent in 2 minutes so we also have another one this one it is in a minute so the next job that you're going to see is the recuring jobs so let's see our dashboard how it looks now for so we have done the fire and forget uh scheduled jobs then we are going to do recurring jobs so you can see this we have done two scheduled jobs so we CED like two or three so we actually deleted this and if you come to our dat database if if we run the fire jobs you'll be able to see some jobs uh the the statement whether they acced so you'll be able to see even the invoice data then the the invocation data the arguments and the created time and date then uh this is the list uh jobs parameters can see here the job cues you see them here the counters can see these are the counters so the next thing that we need to do the next type of job is the recuring jobs so assuming we have a customer with a subscription to our service then we I need to be sending him or her reminder about payments or the invoice itself so this calls for a recuring job so to to do a Rec caring job now come to our our scheduled job then we'll do something called so here we'll do recurring so here we'll just do say recurring recurring job then we say add or update then uh we should be sending that so we don't we don't need the delay time here so you'll be just sending that so we'll do add or update then uh we send an email then how long the recurring time here so we can do Chrome so we do Crown then how the recurring should be daily monthly hourly or even uh minute so it depends with how you will want the recurring job to openen so you can do daily so after that then we can actually execute our job so if I do this we run also our application then H we do fire so so that is our end point so if we execute that can see so if we open our dashboard so my dashboard so we've come to jobs so we have the ined uh processing deleted and even so if you come here you'll be able to see we have uh retries recurring job so you can see we have a recurring job so this recurring job is will be executed next in 15 minutes so if we also run the recurring job again s fire fire and forget you'll be able to see that it is so if we do this but you can actually trigger so if you trigger it now it will be executed so if we trigger it it should be executed so let's come to our jobs uh schedule jobs cured jobs and the recurring jobs so so you have done those types of jobs so the next one is uh continuation jobs so these are job that that is uh whether we have so the next one is the continuation jobs so we come to our sample here then we will do background background job then we do continue job then this will be running without that this will be now a continuation job you will remove that then uh this will be fine so we need the job ID so for that let's let's just have let's variable can say job ID D is equals to so we can be executing this then we say I can do this but now our send email doesn't return anything so implementation go to implementation so you can say this will return a string and uh for that case we can say return return uh so you can say ID should be good so let's return a GID so on our so this return a string so if we come to our hire our job ID will be returning that so we'll do background job with then we say ID then we can do this then we say console sorry so console right line then we say you can do this you say email sent so email sent successfully then we can say with ID so we can just put in the ID here say job ID then you can close this then then you can return so this should be that way this should be this way then we need to put in a comma here then you can return the background job so you can say background job running can say run with ID so you can put in the ID say job ID so if we do that so let's try and execute we see so this is a continuous job there's something wrong somewhere so return this to string so I can say to string so if we do that let's run our job and see so if we do forget forget hangfire so there you are it has return a quid uh so failed cannot add a continuous parent does not exist so so actually this one should be uh so this one should be background job. NQ so you should actually do this background job NQ and Q then we do that then we do this and we close it we do that so there we are so let's see so there we are so if we do forget so background job running with ID 11 so if we do my dashboard uh jobs so we have a waiting so so we have this is email sent with ID so here this is what we have done done so it is it will be running concurrently then we have scheduled and cued jobs recurring so it depend on how you want to run the job so this is just a summary on how we you can actually have a a background jobs with hangfire so we can do that so let's assume we want to run andq let's do that we see I'll get and foret my dashboard jobs you're able to see their schedule jobs are waiting some of the ones that are deleted if it fail you'll be able to see here so this is hire so I think that is it for today's video so if you have any comment comment down below and subscribe to our Channel see you in the next video [Music] [Music] bye yeah
Info
Channel: Macro Code
Views: 140
Rating: undefined out of 5
Keywords: AmigosCode, .NET, .NETCORE, Sundeep, Saradhi, freecodeCamp, ProgrammingWithMosh, MikeDane, IamTimCorey, TeddySmith, FireShip, DaniKrossing, Stefan Mischook, Coder Foundry, Jose Montemagno, .NET 7, Visual Studio, Nick, Nickchapsas, Jake Wright, Keep On Coding, QuestPond, Developer Filip, TechWorld With Nana, Techworld, GotoConferences, CRUD, BoostMyTool, WEBSERVER, .NET CORE, Tech with Tim, Patrick God, Milan Jovanovic, dotnet, IAmTimCorey, Api, Swagger, Hangfire, backgroundJobs, HangfireDashboard, RecurrJobs
Id: Ax5bq7nrDWI
Channel Id: undefined
Length: 39min 27sec (2367 seconds)
Published: Sat Oct 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.