Ioptionsmonitor

Ioptionsmonitor

This is very helpful for a cleaner design not least since because the Core framework gives us the ability to store our configuration on different places. These places for example could be files (. , $"appsettings.{}.json" ), environment variables, command line arguments an something else. So let us explore what paths we are now open to, and if you would try this by yourselves, you could find the sample code on GitHub in a SampleProject.

Core configuration reloadOnChange with

And now let's take a look onto the new and recommended way to get access to the configuration values. This method named Options pattern is, as I think, a much better way doing this.

Should auth options be using IOptionsMonitor? · Issue

I have not been able to follow the changes super close, but I thought IOptionsSnapshot was the go forward strategy (at least in ) to switch the security settings per tenant on each request. That's what the link I pointed to showed. Has that changed? If so what's the new plan?

The key to getting reloads to propagate is to register a listener using the OnChange function of an OptionsMonitor . Doing so will retrieve a change token from the IConfigurationRoot and register the listener against it. You can see the exact details here. Whenever a change occurs, the OptionsMonitor will reload the IOptionsValue using the original configuration method, and then invoke the listener.

In this post I&apos m going to address a limitation with the named options approach seen previously. Namely, the documented way to access named options is with the IOptionsSnaphshot T interface. Accessing named options in this way means they always have a Scoped lifecycle, and are re-bound to the underlying configuration with every request. In this post I introduce the IOptionsMonitor T interface, and show how you can use it to create Singleton named options.

Change the values in the file to value6_from_json UPDATED and 755. Save the file. Refresh the browser to see that the options values are updated:

OptionsBuilder TOptions is used to configure TOptions instances. OptionsBuilder streamlines creating named options as it's only a single parameter to the initial AddOptions TOptions (string optionsName) call instead of appearing in all of the subsequent calls. Options validation and the ConfigureOptions overloads that accept service dependencies are only available via OptionsBuilder.

IOptionsMonitor T is itself a Singleton, and it caches both the default and named options for the lifetime of the app. However, if the underlying IConfiguration that the options are bound to changes, IOptionsMonitor T will throw away the old values, and rebuild the strongly-typed settings. You can register to be informed about those changes with the OnChange(listener) method, but I won&apos t go into that in this post.

So far, we&apos ve mainly looked at two different interfaces for accessing your strongly-typed settings: IOptions T and IOptionsSnapshot T .

You can inject this class in much the same way as you do IOptions MyValues - we can retrieve our setting value from the CurrentValue property.

This extension has the property trackConfigChanges , which looks to be exactly what we&apos re after! Unfortunately, updating our () method to use this overload doesn&apos t appear to have any effect - our injected IOptions still isn&apos t updated when the underlying config file changes.

In order to use both strongly typed configuration and have the ability to respond to changes we need to use the IOptionsMonitor implementations in . We must register a callback using the OnChange method and then inject IOptionsMonitor in our classes. With this setup, the CurrentValue property will always represent the latest configuration values.

Excellent, we can see the new value is returned! This demonstrates that the file is being reloaded when it changes, and that it is being propagated to the IConfigurationRoot.

In our configure method we inject an instance of IOptionsMonitor MyValues (this is automatically registered as a singleton in the MyValues method). We can then add a listener using OnChange - we can do anything here, a noop function is fine. In this case we create a logger that writes out the full configuration.

Options snapshots are designed to be used for transient or scoped dependencies, so you will be fine just using those most of the time. Only in rare occasions when you have to use a singleton service that also needs to have the most updated configuration, you should have the need to use an options monitor. In those cases, note that just switching from snapshots to a monitor will not be enough. Usually, you will have to handle changed configuration in some way (for example clean up state, clear caches etc.). So you should always think about whether you actually need reloadable configuration for everything or if just restarting the application isn’t a viable alternative.

To demonstrate the default behaviour, I&apos ve created a simple Core WebApi project using Visual Studio. To this I have added a MyValues class:

Now the second and more cool way is to use IOptionsMonitor which gave us also access to the configuration. But, in this variant we always have access to the current value also even the values of the configurations will get changed.(This means, another service which also has an injected IOptionsMonitor changes the settings in memory and also when the configuration file was changed). If we want to be informed about the changes of our files, we additionally have to configure reloadOnChange: true in our ConfigurationBuilder.

How could i acess IOptionsSnapshot by a class?
I also have to add it for example in Constructor Than its needed if i create an object to pass the IOptionSnapshot to the Object. But in Controller its not needed i gets the value automatically.

When loading from files, you may have noticed the reloadOnChange parameter in some of the file provider extension method overloads. You&apos d be right in thinking that does exactly what it sounds - it reloads the configuration file if it changes. However , it probably won&apos t work as you expect without some additional effort.

Leave a comment