3.3 KiB
The preferred way to read related configuration values is using the options pattern. For example, to read the following configuration values:
"Position": {
"Title": "Editor",
"Name": "Joe Smith"
}
Create the following PositionOptions
class:
An options class:
- Must be non-abstract.
- Has public read-write properties of the type that have corresponding items in config are bound.
- Has its read-write properties bound to matching entries in configuration.
- Does not have its fields bound. In the preceding code,
Position
is not bound. ThePosition
field is used so the string"Position"
doesn't need to be hard coded in the app when binding the class to a configuration provider.
The following code:
- Calls ConfigurationBinder.Bind to bind the
PositionOptions
class to thePosition
section. - Displays the
Position
configuration data.
In the preceding code, by default, changes to the JSON configuration file after the app has started are read.
ConfigurationBinder.Get<T>
binds and returns the specified type. ConfigurationBinder.Get<T>
may be more convenient than using ConfigurationBinder.Bind
. The following code shows how to use ConfigurationBinder.Get<T>
with the PositionOptions
class:
In the preceding code, by default, changes to the JSON configuration file after the app has started are read.
Bind also allows the concretion of an abstract class. Consider the following code which uses the abstract class SomethingWithAName
:
The following code displays the NameTitleOptions
configuration values:
Calls to Bind
are less strict than calls to Get<>
:
Bind
allows the concretion of an abstract.Get<>
has to create an instance itself.
The Options Pattern
An alternative approach when using the options pattern is to bind the Position
section and add it to the dependency injection service container. In the following code, PositionOptions
is added to the service container with xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure* and bound to configuration:
Using the preceding code, the following code reads the position options:
In the preceding code, changes to the JSON configuration file after the app has started are not read. To read changes after the app has started, use IOptionsSnapshot.