.NET 6 - Background Jobs with Hangfire πŸ”₯πŸ”₯πŸ”₯

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign thank you for watching this video I am Muhammad and today we're gonna be discussing something really important when it comes to building a.net web application web apis MVC applications so on so forth which is background jobs and background jobs are basically tasks restaurants in the background without any user interference we can schedule them we can delay them we can basically make them run whenever we want and because all of these background jobs needs to be handled somehow we're going to utilizing a library which is called Hank fire which basically allow us to do all of these controls on all of these background jobs that we have Hank files are a lot of free functionality which all we're going to be utilizing as well there's a paid version of that of the library where it gives you extra functionalities which we're not going to be delve into within this video If you like this video please like share and subscribe it will really help the channel as well if you like my videos consider supporting me on patreon or on buying me a coffee now grab your cup of coffee and let's get started so we're gonna start first by explaining what is hike fire and what is a background jobs so in essence background shows are basically any task that you want to do without any user interference so for example let's say we want to do a big synchronization about our data but our database we can use a background job we need to make sure that to clean up our database after a big operation a background chart will be able to do that we don't want to send the batch emails to all of our subscribers a background shop can do that so basically in essence it's everything that we want to do from the application side that we don't really need user interference it's basically stuff that runs in the background that it can run basically at night when there's a there's not a lot of people utilizing our system whether there's a lot of resources and which are available all of these can basically do uh can be executed through a background job for example if you want to synchronize our application with a different application we can also utilize the background how to do the synchronization instead of relying on a user to go in and actually do the synchronization themselves so in order for us to do a background job and basically accomplish that within our application there is multiple ways we can actually have some kind of service function to accomplish this we can have like a schedule a clone jobs to do this but one of the main things that it's really powerful thing that we can have within our.net 6 applications or basically any that not application is utilizing a library called Hank fire and basically Hank fire is a way for us to actually trigger those jobs it could be triggered based on time it could be triggered based on recurring times for example or fire and forget so on so forth there's multiple configuration that Hank fire provides us and basically once we are able to actually configure this within our application we can actually utilize all of the tank fire configurations and basically all of this powerful feature that they provide us so for example let's say I want to have a function which basically runs every day around midnight where it actually sends an email to my customers I can utilize tankfire to do that after a user have registered for example what I can do is I can send a batch a welcome email within like five seconds or 10 seconds or a minute after the registration has been completed I can utilize Hang Fire to do that or for example let's say I want to do like a scheduled synchronization a process where actually it runs every few hours and I want basically the application to take care of itself I can utilize also a Hang Fire to all of that so now that we have covered what is a background job and basically what is hang fired now let's start by actually the first creating our web applications and then once we created our application we're going to be actually adding out the new nuget packages that we're going to be needing and then we're gonna be actually see how we can actually configure this so one thing that I want to mention here is we're not going to be actually building a full database integration I'm just going to utilizing an in-memory database but if you want to learn more about databases and how you can integrate it with your.net application I'm going to link a video here somewhere where you can actually go and check that out in for in more details but for now we're just going to be utilizing an in my midi database so first things first I'm gonna go to my terminal and basically what I'm going to be doing is I'm going to navigate to a let's say CD desktop let's say work and inside of that folder I think I have a folder called learning dot not and inside of here I'm going to create a new folder actually I have a folder called Hank fire so let's do that I'm gonna create a new application so I'm gonna make a folder I'm gonna call it Hang Fire Hang Fire web for example and I'm gonna go to that folder Hang Fire web and from here what I'm gonna do is basically I'm gonna create my API so what I can do is I'm gonna say dot not new web API and then I'm going to device the end keyword here and then I'm gonna put I'm gonna call it fire up so now this should take a few seconds to be created okay great now that I have this application up and running or basically created what I need to do is basically to open the visual studio code okay perfect so now that my application is running in Visual Studio code let's give it a few seconds don't ask and let's open this up and from here what I'm going to be doing is I'm going to go to terminal and I'm gonna basically open the new terminal and the first thing that I'm gonna be doing is I'm going to navigate to my fire app and basically I'm just gonna do a simple.net build just simple like that so make sure that the application is building as it should be now that I have built succeeded which is great the next step for me is I need to install some packages and basically here what I need to install are the Hang Fire packages there's going to be multiple packages that I can install so by default Hang Fire actually supports uh integration with a SQL Server where basically it will be able to store all of these tasks or background jobs that we need to do and basically it will be able to check that SQL Server check whatever jobs it has there and it will run them but for the sake of this video I'm gonna be utilizing SQL light well basically I'm going to utilize a sqlite database where a hank file will be able to utilize again you can either have a SQL Server that's out of different extensions out there but uh for this video I'm going to using SQL light instead of SQL Server so first of all if we'll start with the packages so I'm gonna put dotnet new actually not add package I'm the first one is going to be hang fired perfect the next package is going to be like not add package Hank fire dot core and the last package is going to be not add package it's gonna be Hang Fire Dot storage Dot sqlite okay let's do not build just to make sure everything is building perfect now if we go here to our fire app we should be able to see that everything that we see here is basically running so all of the nuget packages that we have installed are available here which is Hang Fire rank file core and SQL SQL Lite great once we have done that the next step is I want to go to my app settings and basically within my app settings what I want to do is I want to add a connection string to my sqlite database where all of my Hang Fire backgrounds task jobs that are going to be stored there so connection strings and inside my connection strings I'm just gonna call it tank fire or I can call it default connection I can call it whatever I want and here I'm just gonna call this database up dot uh DB I can call it whatever I want let's call it fire up because that's the name of the application okay fire up dot DB that's great now let's go back to our program.cs so once you basically once I have installed the packages and basically I have installed my packages the next thing is I need to inform my.net application that there is going to be at the Hang Fire uh nuget package is available and basically what I need to tell it this I need to copy able to configure my Hang Fire whenever the applications start it needs to basically notify Hank fire that the application is running and basically it will need to delegate all of the tax tax I need to delegate all of the tasks for Hang Fire and tank fire will be able to take ownership of those and basically be able to run in the background without any interference from the user and for me to do that what I need to do is I need to basically configure my uh program.cs so now what I'm going to be doing is inside my program.ts first first thing first things first is basically I need to inform my Builder or my services that Hank file is going to be utilized so it's going to be Builder dot services Dot add Hang Fire let's fix those references maybe it took a few seconds for it to actually recognize that there's a package here so once I have added that I'm just going to add the config and then within my config I'm gonna say config first of all is Dot use simple assembly name type so that's going to be the first one the second thing I want to do is basically use recommended serialization serializer settings so that's going to be the second thing and the third thing is I want to specify that I'm going to tell our SQL light SQL light started and for this I need to add a reference and then from here what I can do is simply just refer back to my connection string so I can put Builder dot configuration dot get connection strings and basically I need to pass here my connection string and if we take a look at our app settings we call the default connection so let's copy this oops let's just type it default connection okay so now that I have basically did the first part of my configuration the next part is so basically what I did is I have added my services to my the eye container so basically right now my application will be able to recognize that there's a Hang Fire Service running now what I need to do is basically I need to add it to my middlewares and I need to inform it so for me to do that all I need to do is after the app.nob controllers what I need to do is I need to add dot use Hang Fire dashboard so that's going to be the first one because basically what we need to do is we need to utilize the dashboard to visualize all of my background jobs as well I'm going to be utilizing the app .map fire dashboard so that's gonna be the first configuration so right now this will allow us to basically have a dashboard out of the box that we're actually going to be able to utilize in order for me to see all of the background jobs that's running and to see if there is anything which has failed anything which is actually needs to need my attention and basically everything which is running as it should be so that's going to be the first thing but right now what I did it here is I basically I added it without any security so anyone right now can go to my URL forward shank fire and you will be able to see everything later down in this video what I'm going to be doing is I'm gonna be actually adding some security perspective into it so I'm basically able to secure my uh my Hank fire so only people with specific credentials will be able to actually log in and see all of this background tasks running so for now we're gonna keep this and the next thing is what I need to do is I want to add a model for uh drivers again Formula One themed and basically I'm going to create a controller and see how we can act actually attach all of these background tasks to run within my controllers as well so first of all I'm gonna create a models and when this model is going to be very simple I'm going to create a class and I'm going to call it driver.cs and this is going to be a class so first of all I need to find the namespace which is going to be fire up dot models and then what I need to do is I'm going to put public class driver and this is going to be a very simple model which is going to be your first properties it's going to be of type say good we're gonna put an ID and then I'm gonna put public let's say string name and let's say something which is let's give it a default value prop and I'm gonna say driver number and lastly I just want to give it a status so if it's deleted we'll delete it from here that's it very simple model it's basically container ID a name driver number and the status that's all it is so once I have done that the next step for me is basically just to go in and basically start creating my controller so controllers and I'm gonna be creating and actually what I can do is just duplicate this file so I can call this driver's controller and I'm just gonna update this to Driver's controller drivers controllers drivers controller and driver's controller and I don't really need this so I can just delete it and I'm gonna I'm gonna need this as well so I'm gonna delete it here so now basically I have deleted all of those and I have a basically click controller which I can add whatever I want so as I said previously that we're going to be utilizing an in-memory database here to just to make sure that we are mimicking some kind of a cloud action and basically once we added that in my database we're going to add a couple of actions and then we're going to be seeing how we can actually then we're going to be creating some kind of a service where we're going to mimic an actual service and then we're going to be seeing how we can actually utilize a hank fire to for us to schedule these services and actually utilize them so first things first is let's create this fake or not fake and in-memory database so it's going to be private static list of drivers I'm going to call it the drivers equal a new drivers so that's going to be the first thing now let's fix those references perfect and now what I'm going to be doing is I'm just gonna be adding a couple of of my the actions so I'm gonna put HTTP get or actually let's do the post first so we'll put public I action result and what do we need here we need to make it this ad driver and it's kind of just kind of take a driver a driver I have another video which basically delve into how we could utilize automaper and basically how we can utilize ttos again I'm gonna link it here somewhere where it's actually uh we're gonna be able to see how we can optimize the input of your API if you're interested you can actually check that out but for now we're just neutralizing the very basic simple example of how we can send an object to our API so all I'm saying here is if uh model state dot is valid so in case this is valid what I want to do is basically uh I'm gonna put drivers dot driver sorry dot add driver and I'm gonna be returning here return created at action and I don't have this uh action yet I'm gonna add it it's called get driver and basically what I want to do is I want to pass the new uh object which is going to be without the object here I'm just going to pass the driver.id and I'm gonna pass the object that's it and lastly in case it did not work out I'm just gonna be returning for now just about request okay let's add this new action here which has got driver it's going to be a pretty simple one so HTTP got and I'm gonna put here is public I action result cat driver and this will take basically a good out of type ID and what I'm gonna be doing here is just simply avoid driver equal drivers DOT first or default and I'm just gonna match it on the ID itself equal equal ID and then based on that all I'm gonna say is if it's not null If a driver equal equal to null I'm gonna return not found else or basically yeah else I'm gonna be returning okay with the driver okay that's gonna be the first thing now let's again run this application to make sure that everything is running as it should be so don't not build foreign application is running as it should or building as it should at least so the next step is uh I want to add the delete I think that should be enough so if I come here scroll a bit down and basic import HTTP delete and it's going to be public I action result I'm gonna make delete driver again it's gonna take a good of ID and we're going to do the same thing so far driver equal drivers DOT first or default and I'm gonna map this basically based on the ID equal equal ID and then once I do that I'm gonna say if driver not equal to null or actually let's continue the same pattern as before if it is equal to null what I'm gonna be doing is I'm gonna return not found and let's remove these we don't really need them here let's move this one so this one and in case it has been found what I'm gonna do is I'm just gonna update the status for it so I'm just gonna put a driver dot status equals zero which means it's been deleted and then basically I'm gonna return so delete usually return uh uh what is it no content and that should be it for now so right now it's very simple what we have here is basically we have a mimicking a card operation so we have a model of a driver we have a API endpoint whatever this is a controller where we have a in-memory database for the drivers itself and basically other than that we have three actions which is basically allow us to add a driver delete a driver and basically view a driver all of these three only these three endpoints so now what we're going to be doing is we're going to be creating a service again all of those services are gonna be just like to see how we can actually mimic different services and then we're going to be seeing how we can actually update this controller to take advantage of Hank fires to actually make these service functions to run in the background so inside my main application here so I'm gonna be creating a new folder and this folder I'm going to call it services and this Services folder is going to contain an interface so I'm going to call this interface I service management dot CS so that's going to be the first one and this one basically is going to be like an interface which is going to allow me to add sorry it's going to Define all of the methods so it's going to be pretty simple so let me just fix this one second so here what I'm just gonna say I'm going to put namespace and I'm gonna put here it's gonna be fired up dot services and then I'm gonna Define my interface so it's gonna be public interface and this is going to be I service management and I'm gonna Define a few methods here so void for example we can say send email so that could be one background method that I have another one it could be void for the example of the database so that could be another one the third one it could be void generate I don't know uh merchandise so let's say we have a new driver we want to generate merchandise for the ones they are added we can do that automatically and the last one it could be just synchronization so avoid sync data it could be anything we want so now that I have my interface I'm just going to create a fake implementation again this is all trying to mimic an actual service we just kind of see how we can actually we're not going to be building those services but we're gonna see how we can actually mimic those services and we can utilize them in a background background way oh so that's a background jobs so I'm gonna create a new file and this one is gonna called service management dot CS and inside of here I've got the same namespace which is going to be uh fire up dot services and then it's going to be public class uh it's gonna be service management which is basically gonna utilize the eye service management interface oops service management and here we see it's not happy because basically we didn't do the implementer interface and all that's implemented and what we're going to be doing here is we're just going to be basically instead of throwing an exception what we're going to be doing is we're just gonna do console dot rightline again this is all to try to mimic that functionality uh the triteline and we're going to be saying here that for example generate generate if I can type this morning generate merchandise and here I'm gonna say this is going to be a long running for example method uh task and I'm just going to add as well my the time and date where it's actually started so I'm just gonna put here I'm just gonna put let's see it's going to be daytime so let's do it date time dot now dot to string and here I'm just gonna specify the format that I want so I want it to be in like here really actually let me copy paste it I have it somewhere else here which is in a much better way so it's going to be something like this foreign so what I can do is just copy this one and then update the rest to mimic it so basically it will be something like for this so it's going to be for sending emails I'm just gonna paste this here and I said with this one gonna say send email and this is going to be a short running task sync data this is gonna be a gun sync date oops data and this is going to be a as well shortening task and this one here is kind of called update database and this is going to be a long running task so that we have our service ready basically the next step for us is we need to actually inject this service into our program.cs so it will basically be able to pick up by our service and our di container will be able to recognize that there is this service available so we can utilize so it's going to be very simple we're gonna go to our program.cs and add basically a single line there where basically we are introducing this service to our the eye container so let's go to program.cs and here all we need to do is before we build the application all we're going to add is Builder dot services dot a transient and basically here all that's going to be is basically I service management service management and just fix those references and let's just align this it will look good okay and what I'm going to be doing here as well is first of all let us build this application and make sure that everything is running as it should be so do not build perfect now that it has built successfully the next thing that I need to do is I just want to make sure that let's run it so let's not run it's better dot not run we can see that my application is running and what I'm just gonna do is just take my URL and let's open up in a web browser so let's open up in Firefox okay uh we don't need this right now set up in seconds okay let's choose the dark one next next skip the stop and start browsing okay so all I'm gonna be doing here is I'm just gonna go to my local host and I'm gonna go to Swagger and I'm just gonna put forward slash index.html and it's gonna tell me that it needs to verify my SSL certificate I'm just gonna say accept and now we can see here that I have my uh my endpoints available for me so posting a driver so let's try this out so I'm gonna put the name here sir Lewis foreign number is 44 and it's active oops and now if I click on execute we can see that I got a 201 so now if I try to come here and basically do get driver and let's click on try it out I'm just going to specify id1 execute oh sorry I need to it wasn't the ID this one is the ID so let's copy this ID foreign application is running and basically what I have here is I have uh micro fully operational so the first background job that I want to add is basically I want to specify a task which runs every two hours or basically for the sake of this video I'm gonna make it every few seconds to run and basically this will allows me to make sure that this uh this functionality is kind of basically synchronization process that might happen in the background and basically what I want to do is I want to configure it that it will run every few seconds in order for me to be to basically synchronize my information between the different sources and again this is trying to mimic a real version audio what I have for example my application with a different API which I need to pull the data every viewer every hour two hours three hours depending on the case that I have and basically it will basically in the background without any user interference so in this section right now we're going to be basically adding this and we're going to see how easy it is for us to actually uh create this background jobs with a single line basically so let's do this right now so uh first things first is inside my program.cs first of all I'm gonna be stopping this so Ctrl C and then inside my program.cs after this here I'm just gonna add uh basically the recurring job so I'm just going to say recurring jobs and I'm gonna say dot add and or update and basically and you specify the service that I want to tap into which is going to be the iservice management and basically I'm almost done right now all I need to say is the service Dot which one I want to synchronize the data for example and basically what I need to do right now is I need to specify my current expression and if you don't know what a clone expression is it's basically the date format that I wanna I'm basically informing my application on how often Instagram so basically in Linux we have the Chrome format which basically a transfer any clone jobs that we have and basically it follows a certain like uh milliseconds minutes hours days so on so forth so we're gonna be seeing right now I'm gonna share a website where you can actually go and visualize this it will make it much more easier for it to create your own clone jobs and we're gonna do this right now we're gonna create a crunch up which runs every few seconds again in real life scenario if you're going to synchronize it you might want to do it every hours two hours but for the sake of this video we're gonna make it to run every minute or every few seconds so let's do it this night now so here what I want to do is let's go back to uh Firefox and I'm gonna go to this URL let's go to it and here all I'm gonna put is this one and basically here let's see I don't want this so let's manage the options and confirm choices I don't want any of this so all I want to hear is I want to have a chrome job which runs every let's say every five seconds or every 10 seconds and I think that should be it basically that tells me that I have a clone job that it can run on that or basically I can make it run every second so I can make it like this and this basically will allow me to actually make it run every second um what I'm gonna do I don't think you can see it so I'm gonna just move this a bit to the right that way it will be easier yes now I think you can see it properly and here for example a different ways so what I'm gonna do is I already have this clone job so I already have configured this so if I just type this here I paste it and say describe basically this method will describe this and it tells me that it's gonna run at 0.0 of every minute so that's something good that I can have so I'm going to be utilizing this crunch up here so what I'm going to do is I'm going to go back and for this one here I'm just going to specify the scrunch up and all I'm gonna do right now is I'm gonna run this and I'm gonna open my keep an eye on my terminal or and see basically the logs which is coming out and basically that blocks coming out should basically tell me what that if my services are running in the background or not without me actually doing anything so Natron and now it says it's building and we can see it's running so now let's wait just a minute and we want to see if it's actually gonna be able to trigger this uh background chart on every minute so let's just wait for a minute and let's see if this is gonna run okay so I just realized that I have missed the one item that I needed to add and basically that I needed to make sure that I enabled the Hank fire server so let's do this right now so here what I'm actually going to be adding is it's very simple I'm just gonna put Builder Dot Services dot add Hang Fire server that's all I needed to do and I forgot to add it so now let's try this again let's just run this again do not run and we can see here that my Hang Fire server is running and basically if we just give it a minute to make sure that this uh to see if this will actually start picking up and it starts running so we can see here that my sync data has started and basically we can see that it's actually started triggered it at 8 34.05 now the next one it should be at 8 35.05 with me here we are trying to mimic that every single minute what we are doing is we're actually initiating again and again again as we said previously and the real world scenario is going to be every hour or two hours depending on our needs but for now we can see it actually uh run it again and let's just wait another a bit more time and we're going to be able to see it that is popping up again while we are waiting I think it should take another like a few seconds The Next Step that we're going to be doing is we're gonna be adding this Services into our controller to see how we can actually trigger them directly from our actions based on our needs so let's just give it one more let's open this while it's actually running and what I'm going to be doing right now is I'm just gonna add inside my controllers for example we're adding a driver here I'm gonna add a new item and sorry we can see here that it also ran again at 35.20 milliseconds so again at the same time as the minute before so every minute again is incrementingly adding and adding and adding so that's the behavior that we want to see now let's stop this application because we don't really need it anymore and now what we're going to be doing is we're just gonna enable a job with basically we're gonna see it's whenever we add a driver it's gonna trigger it after a few milliseconds or a delay the job basically what we're going to implement because as we said within Hank fire we have different types we have fire and forget we have delayed jobs we have recurring jobs so on and so forth but for now we're just gonna be running a file and figure job so an easy way to do that is we're just going to put far drive a certain job ID equal say background job okay I think let's fix those references yes sign fire I'm just gonna say here in queue and here it's going to ask me for which from which interface that I wanna utilize I'm gonna say I service management service let's fix those references perfect and here all I'm gonna say is which service I want to utilize and just for here I'm going to say send an email as simple as that now basically what I did is I created a background job where basically I'm able to uh send an email as soon as for example my driver register I did not call it uh basically I did not trigger it directly all I did is I delegate this job to my background services to take care of instead of basically utilizing the main thread for me to do that so what I did is I just dedicated this and basically since I delegated it it's now far Advocate and basically the background job will be able to take care of this functionality so let's try it so let's do not run foreign now we're gonna see again that that background task is going to start let's go to our web browser and I'm gonna come here to my swagger.ui and I'm just going to change this ID instead of uh so Lewis I'm just going to Port George Russell and driver number 63 and now if I execute what I should see inside my terminal I'll basically here let me make sure it's a bit smaller so we can monitor it I should see like a trigger has been initiated to send an email so basically we're triggering the background job so let's see if that will work um okay let's make this a bit smaller so now all I want to see is my terminal below so let's execute this and now it has been executed and because it's fire and forget we should be able to see it here and we can see here that send an email short turning task has been initiated at 8 38 44 seconds and now this is basically it has been delegated to the background job that is running from there so that's one way of doing it another way we can do is whenever I for example I uh let's say I wanna update or basically delete a driver so let's do that so let's go back to my driver controller and let's say whenever I delete a driver what I want to do is I want to let's say I want to synchronize my database so again the way I do that is all I need to do is do recurring jobs dot add or update specify my interface and here I'm going to say I service management and here all I'm gonna say is x dot let's say I want to synchronize data or update my database again anything that I want for example and what I want to do here I want to specify a chrome so I can say cron dot run hourly I can run whatever I want so I'll just put it hourly and here something else what I'm doing is whenever I'm deleting a driver or basically something that's happening what I can do is basically I'm just telling it that it needs to update the data for any single reason and it needs to run every hour after that so just to make sure that the data is running basically all of these scenarios are fake but just so we can see here how we can actually Implement different types of background jobs based on our needs so let's try this again so let's save this and let's stop this and let's go back and run it okay now that it's running uh what we're gonna be doing is uh let's go to our web browser again and let's try to add this driver here and all I'm gonna do is just take the same ID that I have and I'm just gonna delete it so right now if I go to my if I see my terminal I can see that the sync data has been triggered on my send email is running so now what I want to do is if I try to delete it I need to see that the other one which is started to run so I go to drivers try it out specify an ID execute and now basically you can sell I got a no content which is deleted but if I come here right now it should run every hour I should not have make it hourly but basically let's make it a second and let's try it again Dot let's say a minute let's make it every minute okay let's try this again so I need to stop this and let's run this dot not run perfect let's go to our web browser and let's create this user again or basically this driver again so execute and now here all I want to do is first make sure that send the email we can see email is sent and now I'm just gonna delete that driver and basically I want to see what do I get here I got basically deleted okay now let's just wait a minute and while we are waiting I want to show you one other one of the other main powerful feature that tank fire provide which is basically the dashboard so I'm just gonna delete all of this I'm just going to type here Hang Fire and now what we can see here is we can see the dashboard and this dashboard here which is actually one of the main powerful thing that tank file provide is actually manage all of the background tasks see what is running what is currently running how much background jobs are running at the same time and basically would be able to manage all of our background tasks simultaneously so if I come here and I go here I can see that I have the number of jobs and here I can have see those two jobs are running and for example I can see that my sync data and updated database is running or basically are enqueued to be run and before sorry it just because it just popped up we can see that my update database has run after one minute of me triggering this which is exactly the thing that I wanted now let's go back to the dashboard where is it so let's go to the dashboard and here we can see everything which is secured and this is the two recurring job which is happening uh so this one started and this is actually running now and you can see here this is my main server which is going to be the thing which is responsible for running the uh the background jobs we can see I have 20 workers and if I go back here we can see that everything is running I can do a week review day View and basically I'm able to see here the number of succeeded jobs if anything has failed I can see that it actually failed and basically it allows me to have a very good review about basically all of my uh uh background services with my server is handling and here I can see that all of these the history of all of the jobs that has run so update database synchronize data send the email and I can make sure that for example if I click on it I can make sure that it has succeeded uh that what time had started what how much time it took so on so forth and basically all of that is available for us fourthly we're not configuring anything we're not creating those dashboards it's just available for us so you might think to yourself okay that's fine I'm able to do all of this but hang if I leave it like that within the forward slash hunt fire is basically anyone can go into this URL and basically see my background jobs so how can I secure it and that's a very good way a very good to ask question so the way we secured is very easy so what I'm going to be doing is I'm gonna stop this first thing we need to do in order for us to actually secure it is we need to install a new package and this package is going to be basically called uh the dashboard to Hang Fire dashboard so we're gonna put dotnet add where am I typing okay see it here okay.net add package and this is gonna be the Hang Fire dot dashboard dot basic dot Authentication perfect now all I need to do is come here and update this so first of all what I need to do is just specify the same domain that I want which is forward slash Hank fire and then all I need to do is add a new dashboard options and for these options what I want a specify first of all the app name so it could be the the dashboard title I'm just gonna call it dashboard driver's dashboard and then what I need to do is I'm going to specify the authorization and for example for the authorization I'm just gonna put it's equal to new we can see that it's taking uh the authorization will take an array of authorization filters so we're gonna put like this and all I'm going to be doing here is just specify a new Hang Fire custom I think it's called basic authentication I think this is it let's double check it's going to be Hang Fire custom basic authentication filter I forgot the filter and let's see this perfect and then once I do that let's close this and here what I'm just going to put the password for the time being I'll just make it very simple I'm just going to put it password and for the username I'm just gonna make it Muhammad very simple and now all I'm gonna do is just save this and run my application again foreign let's go back to my web browser and if I come here and just put tank fire again we can see instead of directly taking us to the Hank fire dashboard what I'm able to see is the username and a password so I'm just gonna put Muhammad and here I'm just going to put the same password that I added and basically I'm able to see my dashboard again so one thing I want to like to mention is uh do not do not hard code your username and password inside your application YouTube and even do not put in the app settings uh just use secrets and hide it there again this is all for demo purposes and here we can see that the power of Hank fire and how we can actually utilize it in order for us to actually create a background jobs so uh this is what we wanted to cover in this video again this was a very high level overview about the Hank fire implementation how we can actually add it to our web API how we can actually configure it utilize it so on so forth so let's do a quick recap so first things first is we have discussed what as a background job and what is Hank fire then what we actually did is we actually discussed how we can actually integrate this we installed the packages we created a simple crud operations with an in-memory database and then once we have done all of that the next stop is We have basically configured tank fire we created some recurring background jobs we require the delayed jobs we created jobs which is run on a certain uh in a certain amount of time whenever we wanted to and then we saw the dashboard and how we can configure it and make it secure so I really hope this video was helpful if you like this video please like share and subscribe it will really help the channel as well if you'd like to support me please consider supporting me on patreon or buying me a coffee it will be really helpful and again thank you very much for watching and have a great day
Info
Channel: Mohamad Lawand
Views: 32,896
Rating: undefined out of 5
Keywords: .net 6, .net, api, c#, coding, entity framework c#, asp.net core tutorial, .net core, asp.net core, web api, redis cache manager, dotnet core tutorial, dotnet core web api, dotnet core, dotnet performance, background service c#, background task, background worker c#, dotnet background jobs, dotnet backgroundservice, dotnet hangfire, hangfire, background tasks, hangfire dashboard, background jobs, hangfire dependency injection, hangfire c# asp.net mvc, hangfire dotnet 6
Id: Xafuut_KAB0
Channel Id: undefined
Length: 53min 38sec (3218 seconds)
Published: Fri Oct 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.