Quartz.NET as ASP.NET Core Hosted Service - Configuration Basics!
July 26, 2024
• 4,200 views
quartzquartz.netquartz.net scheduler example c#quartz schedulermanage jobsnet corequartz.net tutorial c#dotnetbackground tasksworker servicejob schedulersoftware engineeringtask schedulerdistributed schedulingjob scheduler tutorialsoftware architecturewhat is job schedulerasp.netasp.net coredotnet 7aspnetaspnet coreasp.net core 8how to be a .net developerC# web apirest apivisual studiodotnet web apiwebapiasp net corec#
There's more than one way to set up Quartz.NET in your applications... and that's both good and bad!
The good news: You get flexibility!
The bad news: What the heck is the right way?!
Fear not! I'll walk you through this second option which is probably the most common approach for ASP.NET Core developers. We can get Quartz.NET hooked up nicely into our dependency injection framework!
View Transcript
sports.net is a really popular job scheduling framework that we can leverage in ournet applications including asp.net core hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in a previous video which I'll link up here if you haven't seen it yet I was showing you how you can set up courts.net using one way that you can go configure all of these different things including the scheduler and sqlite in that video I pointed out that we can go set things up more specifically for aspon core so in this video I want to show you a different way that you can go set these things up much more integrated into asp.net core and follows a lot of patterns that if you're familiar with asp.net core this will feel very familiar to you if that sounds interesting remember to subscribe to the channel
and check out that pin comment for my courses on dome train with that said let's jump over to visual studio and check out our asp.net core app okay so the application that we have on the screen is a very simple asp.net core app it does have a really trivial uh hello route we're not even going to be using the route in particular it's not really going to matter at all for quartz in the previous video and this one as well I am going to be using SQL light as the backing store because I'm using persistent storage for quartz jobs that's not really going to matter in this case if you want to go watch the previous video I'll show you how to go set up quarts with SQL light and you can change the different database backing stores it's just using sqlite cuz it was
quick and easy same Concepts apply but go check out that previous video if you want to see how that works in the previous video as well if I go into the project file I mentioned that we had these Nate packages that we need but in this video this package right here quartz. ASP onet core this is going to be the one that you're going to want to add in and that's because the other two specifically these two these quartz packages these are just the ones that I needed to have Quartz in general not specifically with asp.net core for me I was using SQL light same as we're going to do in this video but this one is specifically for hooking up to the I service provider in ASP core jumping back over to program.cs you'll notice that now that we have these quartz packages added
if we go to Builder Services add quartz online six so this is new we get this nice extension method with this fluent syntax this Builder pattern and then we can get the quartz configuration as a call back here right so we get Quartz configuration passed in and we can start configuring this thing in the previous video I also mentioned that quartz does leverage config file so you don't have to do this all in code if you want go have that config file quartz.net has all of this stuff documented on their website so you can see all of the different properties that you can go set and the values that they'll take personally I like doing this in code first so that I can play with it understand it and then worry about configuration files and and my deployments all separately after the fact before we
move on this is just a reminder that I do have courses available on doome train if you want to level up in your C programming if you head over to do train you can see that I have a course bundle that has my getting started and deep dive courses on C between the two of these that's 11 hours of programming in the C language taking you from absolutely no programming experience to being able to build basic applications you'll learn everything about variables Loops a bit of async programming and objectoriented programming as well make sure to check it out these settings here on line 9 and 10 are just to play around with to see that I can set them in the previous video I mentioned that if you wanted to load these properties up so you can use the config file like I was mentioning
or this properties collection is kind of like a dictionary that it's going to read in those key value pairs but you can set them in code as well line 11 till 17 here this is going to be setting up your persistent storage so again previous video we showed how you can go set this up but this is specifically for asp.net core and we use this sort of callback syntax where when we want to go configure X is going to be the config that we have and then we can go set the different properties on that conf fig specifically for the persistent store in this block here right so use properties is going to allow this collection of properties to be on the the job that we're going to have access to I'm using sqlite like I mentioned but the other providers that are available if
you wanted to change that you could go do let's see Microsoft sqlite MySQL Oracle postgress so they're all here and I'm sure there's Nate packages to do more but this is the block where you're going to want to configure that and I'm using the serializer here so so that was the other Nate package I had to include but together this is going to add quartz as auler the other part that you're going to need is going to be the background service so this quartz hosted service is the other thing you're going to want to add because without it it's not going to know to go pick up the jobs and run them automatically it's just going to kind of sit there and not do anything for you this second part is really important to have things up and running you'll need both blocks together but
the settings that I have in here again these are just different options you can play with this is going to say that we're not going to run the posted service and schedule jobs or anything until the application is fully up and running and then I guess on termination as well we'll wait for jobs to complete so there's other settings that you can play around in here these are just the ones that I have for this demo not super important just calling out that you have some configuration again both of these set up ahead of time before we go and build the app off of the Builder we set our minimal API up here and then this next part is going to be what I showed in the previous video it's just going to be having a job that we can go trigger you'll notice that
we don't have access to the scheduler like we did in the previous video so we have to go ask for it and this is going to look a little different because before we just said hey we have a reference to the scheduler directly and here we don't so how do we get it well we can't just ask for theer we can't use this get required service Schuler we need to ask ask for the Schuler Factory and the Schuler Factory has a method that we can call to get the scheduler so if you are going to be playing around with dependency injection and you want to get access to the scheduler you do want to ask for the scheduler Factory in particular once you have the schedule Factory then you can ask for the scheduler itself but once we have that scheduler then we can go
schedule our job directly onto it I'm going to spice this one up a little bit I think co-pilot that seems pretty good with simple schedule we'll add a little interval so that this job will repeat I'll do it every 2 seconds so 5 seconds after the application starts up we will go run the test job that we have every 2 seconds repeats forever and if we scroll down a little bit lower this is a really simple job that just says hello world we're ready to go run this thing prove that it works I want to call out that I've already set up the schema for my persistent store if you have not done that and you're following along you will want to go run the SQL that is what you're using for your backing store on their website quartz.net if you go to their GitHub
they do have a database folder with the scripts that you need to run to quickly show you oh I've already moved it down to here this one here is going to be what you need for sqlite like I said you're not going to want to pause this video and try to copy a character for character go to their GitHub they have all of the SQL that you need to go run with that said we have our schema set up from ahead of time we have all of this config good to go we should be able to go run this thing if our app starting up I'll pull the console to the front there's the hello world and then again and again so this will now keep going every couple of seconds and print out to the console so this is essentially how you go set
up courts.net specifically with asp.net core we get it added into the hosted services and everything it is going to run this in the background for us and in this particular case use sqlite as the backing store okay so if we have a quick look at our test job here it's obviously really simple I'm just printing hello world to the console if we wanted to do something more complicated if we wanted to parameterize this how can we go about and pass other data into our quartz jobs well if you want to see how that works and the different options that we have available you can go ahead and check out this video next thanks and I'll see you next time
Frequently Asked Questions
What is Quartz.NET and how is it used in ASP.NET Core?
Quartz.NET is a popular job scheduling framework that I can leverage in my ASP.NET Core applications. In this video, I demonstrate how to integrate Quartz.NET into an ASP.NET Core app, allowing me to schedule and manage background jobs effectively.
Why should I use code-first configuration for Quartz.NET instead of a config file?
I prefer using code-first configuration because it allows me to play around with the settings and understand how they work before worrying about configuration files and deployments. This approach gives me more flexibility during development.
How do I access the scheduler in Quartz.NET when using ASP.NET Core?
These FAQs were generated by AI from the video transcript.In ASP.NET Core, I can't directly access the scheduler like I could in previous versions. Instead, I need to request the scheduler factory and then use it to get the scheduler. This is part of the dependency injection pattern that ASP.NET Core follows.
