Class ConfigurationManager
- Namespace
- Dynamicweb.Configuration
- Assembly
- Dynamicweb.Core.dll
Class for managing configuration values. Uses a provider that defines where configuration values are stored. The default configuration manager is accessed through SystemConfiguration which uses the XmlConfigurationProvider to return values from settings stored in /Files/*.config files.
public class ConfigurationManager
- Inheritance
-
ConfigurationManager
- Derived
- Inherited Members
- Extension Methods
Examples
namespace Dynamicweb.Configuration.Examples;
public static class ConfigurationSample
{
public static void Configure()
{
// Setting a configuration value
SystemConfiguration.Instance.SetValue("/Globalsettings/Modules/MyModule/Feature", true);
// Getting a configuration setting as a string value
string dbserver = SystemConfiguration.Instance.GetValue("/Globalsettings/System/Database/Server");
// Getting a configuration setting as an integer value
int maxAttempts = SystemConfiguration.Instance.GetInt32("/Globalsettings/Modules/MyModule/MaxAttempts");
// Getting a configuration setting as a boolean value
bool useFeature = SystemConfiguration.Instance.GetBoolean("/Globalsettings/Modules/MyModule/UseFeature");
// Getting a configuration setting using TryGet
if (SystemConfiguration.Instance.TryGet("/Globalsettings/Modules/MyModule/ApiKey", out string? apiKey))
{
if (!string.IsNullOrEmpty(apiKey))
{
}
}
}
}
Remarks
Implement IConfigurationProvider to add configuration providers that can store and retrieve settings from other sources.
Methods
AddProvider(IConfigurationProvider)
Adds an instance of IConfigurationProvider. This enables multiple providers to contribute the the same ConfigurationManager.
public void AddProvider(IConfigurationProvider provider)
Parameters
providerIConfigurationProviderThe provider to add.
AddProviderPriority(IConfigurationProvider)
Adds an instance of IConfigurationProvider as the first provider in the provider list giving it priority over the system configuration provider. This enables multiple providers to contribute the the same ConfigurationManager.
public void AddProviderPriority(IConfigurationProvider provider)
Parameters
providerIConfigurationProviderThe provider to add.
Contains(string)
Determines whether the configuration contains the specified key.
public virtual bool Contains(string key)
Parameters
keystringThe key that represents the value, e.g. "/Globalsettings/System/Database/Trusted"
Returns
- bool
trueif the configuration contains an entry with the specified key; otherwise,
.false
GetBoolean(string)
Gets the bool value for the specified key.
public virtual bool GetBoolean(string key)
Parameters
keystringThe key that represents the value to get, e.g. "/Globalsettings/System/Database/Trusted"
Returns
- bool
The value converted to bool if the key exists and is a valid boolean string in configuration file (True, 1, On), otherwise
.false
GetDouble(string)
Gets the double value for the specified key.
public virtual double GetDouble(string key)
Parameters
keystringThe key that represents the value, e.g. "/Globalsettings/System/Database/Trusted"
Returns
GetInt32(string)
Gets the int value for the specified key.
public virtual int GetInt32(string key)
Parameters
keystringThe key that represents the value, e.g. "/Globalsettings/System/Database/Trusted"
Returns
GetKeys()
Gets all keys that can be used to get or set data.
public ICollection<string> GetKeys()
Returns
- ICollection<string>
keys
GetValue(string)
Gets the string value for the specified key.
public virtual string GetValue(string key)
Parameters
keystringThe key that represents the value to get, e.g. "/Globalsettings/System/Database/Trusted"
Returns
SetValue(Dictionary<string, string>)
Sets a collection configuration entries.
public virtual void SetValue(Dictionary<string, string> keyValues)
Parameters
keyValuesDictionary<string, string>The key/value pairs to set.
SetValue<T>(string, T)
Sets value of the entry with the specified key.
public virtual void SetValue<T>(string key, T value) where T : IConvertible
Parameters
keystringThe key that represents the value, e.g. "/Globalsettings/System/Database/Trusted"
valueTThe value of the element to add to the configuration file. Should be simple value types like string, bool, int or double.
Type Parameters
TThe type of the value.
TryGet<T>(string, out T?)
Attempts to get the value associated with the specified key.
public virtual bool TryGet<T>(string key, out T? value) where T : IConvertible
Parameters
keystringThe key that represents the value, e.g. "/Globalsettings/System/Database/Trusted"
valueTWhen this method returns, contains the value associated with the specified key, or the default value of the type if the key was not found.
Returns
- bool
trueif the specified key was found; otherwise,false.
Type Parameters
TThe type of the value.