I have a .Net6 Console application where I need to retrieve an array of objects from AppSettings.json. Currently I am using an 'options' class to retrieve the array.
class ConfigOpts { class subOpts { /* property accessors */ }, public List<subOpts> SubOpts {get;set;}}
and
var builder = new ConfigurationBuilder().SetBasePath(...).AddJsonFile(..);
IConfiguration config = build.build();
ConfigOpts opts = new ConfigOpts();
config.GetSection("ConfigOpts").Bind(opts);
Gives me an opts objects containing a list of subOpts.
One of the sub opt fields needs to be decrypted and encrypted at load/save time. So I would like to use a CustomConfigProvider and do the decrypt in the Load() method, and encrypt in a CreateSave() method.
I have amended my code to add a new class ConfigOptsProvider:ConfigurationProvider,IConfigurationSource and created an extension method AddConfigOptsProvider which is called on the binder above.
This works and I can hit the load() function in the config provider.
My two questions are:-
In the Load Method, how do I read the AppSettings.json file (I've looked for documentation but found examples for reading databases and other files but none for AppSettings.json).
Also, how do I call the CreateSave() function later?
Regards.
Update I noticed as I have the ConfigOpts class that allows me to access the settings in a typed manner, it might be useful to try and use it.
In the load I have tried adding the following to the Load() method.
((IConfiguration)this).GetSection("ConfigOpts").Bind(_configOptions);
This code is what I originally used (in the first example above). Unfortunately one cant cast to IConfiguration.
It would be great if this is all I need to do.