BrandGhost

DYNAMICALLY Load Settings With IOptions in ASP NET Core

Stop right there! Stop it! Stop hardcoding your values into your ASP NET Core applications! Seriously though -- we have a better way. We can leverage our appsettings.json file in combination with IOptions to pull values out of our configuration. This allows us to avoid having to rebuild an entire application to change values. But what if your app is already running?! Don't worry -- there's a solution for that. Stay tuned in this video to see how we can go from hard-coded values to IOptions to dynamically loading those same settings on the fly!
View Transcript
stop hardcoding your values inside of your asp.net core applications seriously we have better options that we can use for this sort of thing hi my name is Nick centino and I'm a principal software engineering manager at Microsoft if you found that you keep hardcoding strings in different parts of your application and that if you want to go change these things you really have to either go rebuild your code or redeploy your applications don't worry there's a better solution than having to do that in this video I'm going to walk you through the ey options and I options monitor interfaces that we have access to that make this a lot easier for us a quick reminder that if you like this kind of content subscribe to the channel and check out that pin comment for my courses on don't train with that said let's jump over to visual studio and check out a very simple ASP core app and what we can do here all right so on my screen what I've done is I started with the weather forecast sample application that we get from Visual Studio I stripped out all of the code that's not necessary and transformed the one route that we have into a hello get request that we can make and you'll notice that it doesn't do a whole lot it's very simple and it's using minimal apis here what I'm doing for this route is I have a new object that I'm returning it's going to have a string that's hardcoded as well as an integer here that's hardcoded as well so what we're going to look at is if I go run this it's not going to be very surprising but if I get it going here and pull it up on my screen we can see that I do get a Json response that has string as the name for this one with some string right it's the values that are hardcoded here in the route and integers for 20 and if I want to go change these the only way to go do that right now is that I have to go basically into the code I can go 1337 here and if I wanted to have this take effect I would have to go rebuild and redeploy my application if this were in production obviously running from Visual Studio makes this extremely trivial right I just press play but the point here is I have to go change the code because it's been hardcoded in and if you have different things like that that are in your application and you want to start decou in the build and release and deployment process from being able to make these types of changes there's something better that we can do here what I'd like to do now is introduce this idea of eye options and what we're able to do with eye options is start pulling data out of our app settings Json file so if you're not familiar with that I'm going to jump over to app settings Json on the left side of Visual Studio here in the solution Explorer and you'll see that I've added in and if you're not familiar with this I guess I should mention the rest of this stuff that you see right now is what came by default and what I've done is added in this settings section it's hierarchical right so I have settings and then I have some properties inside of it and what I'm going to do is basically allow us to pull this data out of this file so that we can leverage it in our application therefore not hardcoding things so we don't have to go rebuild our application but we will have a configuration file that we could put onto the machine instead there are going to be some drawbacks still it's not going to get us all the way there but this is just a stepping stone to not have it hard coded in the code before we move on this is just a reminder that I do have courses available on dome train if you want to level up in your C programming if you head over to dome 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 object-oriented programming as well make sure to check it out you'll see that I do have the string property integer property that I want to be able to represent I put some different values in here right so this is the string in 123 if you go back to the code that's different than what we saw with some string in 1337 I've done that just to show you that we will be taking on these new values so what I'm doing is I'm going to allow us using the dependency injection framework we can ask for eye options and I have to go make this settings option type now what I would like to do and spoiler alert this doesn't work we'll see why in just a moment is I'd like to introduce a record type here called settings options and then I will have the string property integer property as well that we can work with but this won't work I will show you very soon what we need to do Beyond just doing this is basically allow us to get this from dependency injection so I do have to go add in on the dependency injection part up at the top that we want to configure settings options and it's going to pull from the section called settings and again just to show you if I go back to app settings settings that name that we saw in the code is what we're like the area the section essentially that we're pulling from in this app settings Json file this part allows us to get the mapping inde dependency injection and then we'll see here that on the minimal API dependency injection is going to be able to provide this for us so that we can ask for it now that means what we can do is ask for settings dot ask for the value and then we can get the string property here then we can do the same thing but we can ask for the integer property on the second one now if I go run this we should be able to see that we get the values pulled directly from the app settings config file and surprise like I said it's actually not going to work because we can't use the record right I mentioned that before so this is what I would want to do but we can't use records and it's because we need to be able to have a public parameterless Constructor so this option even though it seems like a very good fit is out we can't use that but what we can do is use a class we can use a class with a knit instead of Setters on here so if we want to be able to create this instance and not have public Setters on them we can use a knit so during object initialization this can get set so if I go run this now we should see that in fact this does work and there we go so we see this is the string in integer 1 2 3 that's being pulled rate from here so lines 14 and 15 and to prove it right so this is the string integer 1 2 3 we can see this is the string and integer of 1 2 3 so essentially what we've been able to do is configure dependency injection to have settings options this class that we've created map to a section right that's what this part does in our app settings Json file then we use dependency injection to pass them back as the response and of course we can do other things with this so if you wanted to be able to configure logging right that's actually what we see inside of app settings Json one of the major purposes for this is that you can change logging without having to go rebuild your application but there are tons of other things that you might want to be able to configure without a whole rebuild you might be saying Nick you did say there was going to be a drawback to this St it's not going to get us all the way and what did you mean by that it's a great question because we're going to jump into that next the problem is still that we need to be able to restart our application so you need to be able to go essentially push this file to wherever your server is running restart the application for to go take on the new settings but we do have a little trick that we can do to go improve this and that's going to be using ey options monitor if I go back to the code here so on line 10 I can change i options to i options Monitor and now one more thing that we have to go do is not ask for Value but instead we ask for the current value and I'll do that in both spots here now what we're able to do is restart this thing and it's going to look very similar right it's not going to be that exciting it's basically going to be the last result so we just saw so you say hey Nick that didn't do anything but something cool that we can do now is if I keep this thing running if I go back to app settings Jon and I put in uh 1337 here and then I put in Dev leader for this part right I save that if I go back to here you saw that I didn't restart the application I didn't rebuild anything press enter boom we can pull these in without having to restart anything in your setup again I don't know what your environment looks like how you're deploying all these things that's for you to figure out but what we're able to do is basically have a way to push this file to where your server is running and then this is able to dynamically reload for us and pull stuff from the config file something that you'll still want to keep in mind even with this in place is you may have things that were configured on Startup or those values were cached at startup when they were pulled from the config file and even if you do something like this essentially your code is not actually going back to the IE configuration or the eye options in this case what it's doing is just reading your cached value in those particular cases you wouldn't be getting this sort of hot reload functionality and you may have to go consider what things you do want to have hot reload and what things you do want to basically cach and have as a one-time setup it's just something important to keep in mind because it's not like automatically by going to i options monitor suddenly everything in your program will dynamically change there might be some things that you don't want to dynamically change or some things that you designed in a way that are very difficult to Dynam dynamically change even though you can read the config on the Fly you might have to rebuild Big parts of your application in code at runtime to be able to go say hey look the config changed let's go reconstruct all these objects this is a tool that you can leverage if you are sort of deep in your application design and that you're not ready to support this it might be something you can consider for next time or think about refactoring parts of your code to be able to get you to leverage this if it's going to be helpful now for some of my smaller projects that I go create if I have some downtime because I need to go basically redeploy a whole server it's not a real big deal but if you're running real systems and you need to keep that uptime you may want to consider something like this because you don't technically have to have downtime to go reload that config if you thought this was interesting and you want to see more about building asp.net core applications you can check out this video next thanks and I'll see you next time

Frequently Asked Questions

What are IOptions and IOptionsMonitor in ASP.NET Core?

IOptions and IOptionsMonitor are interfaces that allow us to manage application settings in a more dynamic way. Instead of hardcoding values into our application, we can pull configuration data from our appsettings.json file, which makes it easier to change settings without needing to rebuild or redeploy the application.

How can I dynamically reload settings without restarting my application?

By using IOptionsMonitor instead of IOptions, I can access the current value of my settings dynamically. This allows me to make changes to the appsettings.json file and see those changes reflected in my application without needing to restart it, as long as the application is still running.

Are there any limitations to using IOptions and IOptionsMonitor?

Yes, there are limitations. While IOptionsMonitor allows for dynamic reloading of settings, some values may be cached at startup, meaning they won't automatically update unless I specifically design my application to handle those changes. It's important to consider which settings need to be dynamic and which can remain static.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload