Class ConfigurationBuilderExtensions
- Namespace
- OpenSettings.Extensions
- Assembly
- OpenSettings.dll
Provides extension methods for IConfigurationBuilder to retrieve settings from the configuration and build application settings asynchronously.
public static class ConfigurationBuilderExtensions
- Inheritance
-
ConfigurationBuilderExtensions
- Inherited Members
Methods
BuildSettingsAsync(ConfigurationBuilder, OpenSettingsConfiguration, string, CancellationToken, params Type[])
Builds and configures settings asynchronously for the application based on the specified service configuration.
The BuildSettingsAsync(ConfigurationBuilder, OpenSettingsConfiguration, string, CancellationToken, params Type[]) method handles the initial setup of settings for the application, with behavior changing depending on whether the settings service configuration is set to "Provider" or "Consumer". This method is responsible for syncing application data, generating settings-related files, and adding them to the configuration builder.
The generated settings files are made available through the IConfiguration interface, allowing them to be easily retrieved by the application. This method is typically used during application startup to ensure that settings are properly loaded and integrated into the system.
If the settingsTypes parameter is specified, only the types inherited from ISettings will
be treated as settings and managed by the OpenSettings. If no types are provided or if the parameter is null, the library
uses reflection to identify and manage all classes inherited from ISettings within the current assembly.
This method is asynchronous and supports cancellation via a cancellationToken. It also accepts optional
parameters such as a logger, environment name, and a list of settings types to be configured.
public static Task<IConfigurationBuilder> BuildSettingsAsync(this ConfigurationBuilder configurationBuilder, OpenSettingsConfiguration openSettingsConfiguration, string environmentName = null, CancellationToken cancellationToken = default, params Type[] settingsTypes)
Parameters
configurationBuilderConfigurationBuilderThe configuration builder used to add the settings to the application's configuration.
openSettingsConfigurationOpenSettingsConfigurationThe settings service configuration determining the behavior (Provider or Consumer).
environmentNamestringAn optional environment name to customize settings per environment.
cancellationTokenCancellationTokenA cancellation token to cancel the operation.
settingsTypesType[]The types of settings to build and configure. Only classes inheriting from ISettings will be treated as settings.
Returns
Exceptions
GetSettingOrDefault(IConfiguration, Type)
Retrieves a setting from the configuration or returns the default value if not found.
Retrieves a setting of the specified type from the configuration. If no setting is found, it returns null or the default value of the type.
public static object GetSettingOrDefault(this IConfiguration configuration, Type type)
Parameters
configurationIConfigurationThe IConfiguration used to retrieve the setting.
typeTypeThe Type of the setting to retrieve.
Returns
- object
The setting of the specified type, or
nullif not found.
GetSettingOrDefault<T>(IConfiguration)
Retrieves a setting from the configuration or returns the default value if not found.
The GetSettingOrDefault<T>(IConfiguration) method retrieves a setting of type T from the configuration.
If the setting doesn't exist, it returns the default value for the type. The type T must implement ISettings.
The non-generic GetSettingOrDefault(IConfiguration, Type) method retrieves a setting by the provided type.
If no setting is found, it returns null or the default value of the specified type.
public static T GetSettingOrDefault<T>(this IConfiguration configuration) where T : ISettings
Parameters
configurationIConfigurationThe IConfiguration used to retrieve the setting.
Returns
- T
The setting of the specified type, or the default value if not found.
Type Parameters
TThe type of setting to retrieve, which must implement ISettings.