Option pattern allows you to read values from configuration file ( i.e. AppSettings.json) in a strongly typed model.
It's a 4-step process:
1. add values in AppSettings.json with appropriate section and setting keys.
2.you need to create a class with similar property names, mentioned in AppSettings.json file.
4.Finally, we need to add dependency of IOption<MySettings> in Controller constructor to use settings in Controller.
Note: Using IOption will not allow us to change settings in AppSetting.js while application in running mode. we need to restart app to get updated settings.So to overcome this limitation of IOption<T> we can use IOptionSnapShot<T> which will update option model as we change settings in AppSettings.json without restarting application.
Property name in class and keys defined in appsetting must match otherwise you will get null value for the unmatched property. or entire option object null if section and keys are not matched.
No comments:
Post a Comment