Managing .NET App Configuration With The Options Pattern In ASP.NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is Milan and in this video we're going to be talking about reading application configuration values and all of the options that we have available to do that I prepared a simple application where we have a class representing our application options it only has one string property which is called example value this property is mapped to our section inside of our application settings Json file here I have a configuration section with the name of application options which is the same name as the class representing our configuration values and the only value inside of this section is the example value which again matches the name of the property on our application options class there are a few ways to bind this Json value into our application options class I'm going to use the simplest one first and later in the video I'm going to show you the more complex examples that might be interesting to you so the simplest approach would be using Builder Services calling the configure your method where we specify the application options as the generic argument and we have to provide the configuration section that we are going to match to our application options to do that I'm going to say Builder configuration get Section I need to pass in the key which represents the name of our configuration section and because it is the same name as the name of our class I'm going to use the name of operator to pull this value and fetch the configuration section so this takes care of configuring our application options and binding it to our Json values inside of app settings.json so how do we actually use this let's Define a simple get endpoint that we're going to use for getting the options value I'm going to name the route options and let's quickly do the implementation to get the application options at runtime we have to inject an instance of the i options interface and specify the application options as the generic argument and I'm going to say options and now on the i options instance I have access to the value property which is going to give us our instance of application options and now we can access the properties on our application options which is going to contain our configuration values I'm going to create a simple Anonymous object that is going to represent our response and I'm going to assign it a single property which is going to contain our application options example value and let's return this from our API by calling results.okay and specifying response if I start the application and head over to postman here I already prepared a get request for fetching the options from our application so when I send this request I get back a Json response containing our configuration value and you can see that the example value in this case is current value is 1. if I go back and I head over to the app settings.json value and here if I say that the current value is 2 and I save it and now I head back to postman and I send the request to our API again you can see that the response that we get back still says that the current value is 1. this is because the i options interface is registered as a Singleton service at the start of the application so it's only going to read the configuration section from our app settings.json filed once it's going to Cache that value and it's going to reuse the same instance throughout the lifetime of our application to be able to read is the newest value from the application settings we would have to restart the application and then you would get the latest value in the response if you need live reloading capabilities there are some options that you can explore and I'm going to show them to you now so instead of injecting i options there are two more interfaces that we can inject one of them is the i options snapshot and again we're going to specify application options as the generic argument and let's name it options snapshot another interface that you can inject is the i options Monitor and I'm again going to specify application options and name it options monitor I option snapshot and I options monitor are fundamentally different from the i options interface I'm first going to show you how they actually work and then I'm going to explain the specifics behind all of them I'm going to slightly modify our response object and I'm going to give it a few properties the first property is going to be called options value and I'm going to pull it from the i options interface then I'm going to have the snapshot value and I'm going to pull it from option snapshot then I'm going to add the monitor value and I'm going to pull it from options monitor current value this time and then I can access the example value property so let me start the application and let's see how this is actually working I'm back in Postman and I'm going to send the get request to our API again and you can see that this time we get a response with three values the first value is coming from the i options interface the second value is coming from the I option snapshot interface and the last value is coming from the eye options monitor what is interesting now is that the eye option snapshot and ioptions monitor have support for reloading configuration values so if I go back to our project and inside of the app settings.json file I replace the current value as 2 with let's say the current value is free and save that and now I send the request to our API again you can see that this time we get a different response the value coming from the i options interface is still the same because it is configured as a Singleton and it is cached for the lifetime of the application but you can see that the values coming from I options snapshot and I options monitor are refreshed and they do return the latest value that is present in our application configuration I want to highlight the differences between iops and snapshot and I options monitor the I option snapshot interface is configured as a scope service which means it's going to be resolved once inside of the current application scope in our minimal API example the scope would be one HTTP request High options monitor on the other hand is configured as a Singleton service but the current value property is always going to read the latest configuration value if you're enjoying this video so far make sure that you smash that like button and also subscribe to my channel so that you don't miss any of my future videos now I mentioned at the start of the video that I'm going to show you an additional way to configure your application options I'm going to add a new class to our project I'm going to name this class application options setup and I'm going to use this class to configure our options value I need to inherit from the iconfigure options interface and I'm going to specify the application options as the generic argument so let's implement this interface one good thing about this approach is that it supports dependency injection so I can do something like this I can inject an instance of eye configuration which I'm going to use to access the actual configuration section that contains the values of my application options and the way that you bind the values from the configuration section to your application options is by calling in configuration get Section you specify the name of the section in our case this is name of application options and then you call the bind method and you specify the options instance as the argument so what this is going to do is it's going to get the configuration section and it's going to bind it to the application options instance now to make this work you need to change the configuration in program.cs so we're no longer going to be configuring our options by calling configure we're going to use a different approach we're going to say Builder Services configure options this time and we're going to use the generic version and now I need to specify my application options setup and what this is going to do is tell the application that when it's resolving an instance of i options of application options you have to use this class the application option setup to configure that options instance the problem here is that we are calling bind which is going to bind the configuration section to the options and that is fine but it's not going to register the change tracking capability that is available in the example from the start of the video where we called the configure method on the iservice collection if you enjoyed this video then I'm sure you're going to love the two videos that you can see on the screen right now so go ahead and watch them and until next time stay awesome
Info
Channel: Milan Jovanović
Views: 25,365
Rating: undefined out of 5
Keywords: options pattern, options pattern in asp.net core, options patterns, options pattern c#, options pattern azure functions, options pattern for configuration in asp.net core, options pattern dependency injection, ioptions, ioptionssnapshot, ioptionsmonitor, options pattern .net, options pattern appsettings, options pattern app configuration, options pattern configuration, clean architecture, asp.net core, .net, app configuration, appsettings.json
Id: wxYt0motww0
Channel Id: undefined
Length: 9min 1sec (541 seconds)
Published: Fri Dec 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.