C# Class Azavea.Open.Common.Config

This class reads configuration parameters from a standalone config file identified in the app.config or web.config's "appSettings" section.
Afficher le fichier Open project: azavea/net-om-utils-common Class Usage Examples

Méthodes publiques

Свойство Type Description
Application string
ConfigFile string
ConfigXmlDoc System.Xml.XmlDocument

Protected Properties

Свойство Type Description
_innerXmlByComponent string>.IDictionary
_log log4net.ILog
_orderedParamsByComponent IDictionary>>
_outerXmlByComponent string>.IDictionary
_paramsByComponent IDictionary>

Méthodes publiques

Méthode Description
ClearConfigCache ( string appName ) : void

Allows you to explicitly remove a config from the cache, for example during unit testing, or any time when you know the config file in the cache is no longer valid.

ComponentExists ( string component ) : bool

Method to check if a config section exists for a component, prior to calling GetConfigXml or GetParametersAsHashTable (which throw exceptions if you request an invalid component name).

Config ( string appName ) : System

Constructs a config class given the "appName", or the key to look up in the app/web.config's "appSettings" section. The value for that key is the path to the config file we're interested in.

Config ( string appName, XmlDocument configXml ) : System

Construct a config directly from an XML document rather than from a file.

Config ( string configFileName, string appName ) : System

Constructs a config class given a specific config file to load.

Config ( string configFileName, string appName, XmlDocument configXml ) : System

Construct a config class. The app name is used for logging, and to get the config file name if the file name was not specified and the XML was not passed directly. If you provide the XML, the app name is just used for logging, and the filename is stored but not used for anything (and may be blank).

Equals ( object obj ) : bool

Two Configs with the same config file and appname are Equal.

GetConfig ( string appName ) : Config

This allows you to avoid reading the same config file over and over again. Since Config objects are read-only, we can read the file once and hand the same object out over and over without worrying about threading issues.

GetConfigInnerXml ( string component ) : string

Gets you the XML section for the component, allowing you to do any special parsing that may be necessary. This includes ONLY the children of the "component" tag, and will not have any text included in that tag.

GetConfigXml ( string component ) : string

Gets you the XML section for the component, allowing you to do any special parsing that may be necessary. This includes the "component" tag, and any text included in that tag.

GetHashCode ( ) : int

Hash code, based on the config file name and app name.

GetParameter ( string component, string parameter ) : string

Returns the config parameter for the given component. Throws an exception if there is no such parameter. If you want to know if the parameter exists, call ParameterExists(...).

GetParameter ( string component, string parameter, string defaultValue ) : string

Similar to GetParameter, except rather than throwing an exception if a parameter doesn't exist, returns the default value.

GetParameterAsBool ( string component, string parameter ) : bool

Similar to GetParameter, but converts the type for you (if possible, throws if not).

GetParameterAsBool ( string component, string parameter, bool defaultValue ) : bool

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterAsBool ( string component, string parameter, bool defaultValue ) : bool?

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterAsDouble ( string component, string parameter ) : double

Similar to GetParameter, but converts the type for you (if possible, throws if not).

GetParameterAsDouble ( string component, string parameter, double defaultValue ) : double

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterAsDouble ( string component, string parameter, double defaultValue ) : double?

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterAsInt ( string component, string parameter ) : int

Similar to GetParameter, but converts the type for you (if possible, throws if not).

GetParameterAsInt ( string component, string parameter, int defaultValue ) : int

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterAsInt ( string component, string parameter, int defaultValue ) : int?

Similar to GetParameterWithDefault, except converts the type of the value (if present).

GetParameterWithSubstitution ( string component, string parameter, bool tolerant ) : string

Similar to the regular GetParameter method, except it will substitute environment variables in the values if present.

GetParameterWithSubstitution ( string component, string parameter, bool tolerant, string defaultValue ) : string

Similar to the regular GetParameter method, except it will substitute environment variables in the values if present.

GetParametersAsDictionary ( string component ) : string>.Dictionary

Gets you a Dictionary of all the parameters for the component.

GetParametersAsList ( string component ) : string>>.IList

Gets you a list of all the parameters for the component as key-value-pairs. This preserves the order of parameters from the config file.

ParameterExists ( string component, string parameter ) : bool

Method to check if a parameter exists, prior to calling GetParameter (which throws exceptions if you request an invalid parameter).

ReplaceEnvironmentVariables ( string val, bool tolerant ) : string

If there are any environment variables (in the form %VAR%) in the input string, replaces them with the values from the environment. This method can be tolerant or intolerant of errors, so: "abc" -> "abc" "abc%windir%abc" -> "abcC:\WINDOWSabc" "abc%abc" -> exception (intolerant) or "abc%abc" (tolerant) "abc%nosuchvar%abc" -> exception (intolerant) or "abc%nosuchvar%abc" (tolerant) "abc%windir%abc%" -> exception (intolerant) or "abcC:\WINDOWSabc%" (tolerant) Calling this method with "false" for tolerant matches the previous behavior. Methods like File.Exists do not parse environment variables, so this method should be called before attempting to use filenames etc.

ToString ( ) : string

Returns the config file name and app name./>.

Méthodes protégées

Méthode Description
Config ( string configFileName, string appName, XmlDocument configXml, bool treatMissingFileAsEmpty ) : System

The default behavior of Config is to throw an exception if the config file does not exist. This constructor allows a child class to override that, in which case a missing file will be treated as though it were empty (no values loaded, but no exception thrown).

Config ( string configFileName, string appName, XmlDocument configXml, bool treatMissingFileAsEmpty, string>.IDictionary paramsByComponent, string>.IDictionary orderedParamsByComponent, string>.IDictionary outerXmlByComponent, string>.IDictionary innerXmlByComponent ) : System

This signature lets a child class provide more complicated or specific types of collections. Reminder: You probably want to use CaseInsensitiveStringComparers in your dictionaries!

MakeParameterCollection ( ) : string>.IDictionary

This is a ugly hack at the moment. This allows child classes to override the internal type of collection we use. This will be removed when we refactor the architecture to have an abstract base class so we can have a "writeable" version of Config that does not conflict with the implementation of this "readonly" Config.

ReThrowException ( string msg, object paramList, Exception orig ) : void

All the checks for null are because there was an issue where something about a caught exception was null, which caused the error handling code to bomb. Since error handling code is the worst place to bomb (you lose the original exception), to be safe we manually convert null values into "null" strings.

Private Methods

Méthode Description
ParseConfigXml ( ) : void

Reads the XML and populates the various attributes based on it (lists of params, dictionaries of params, etc).

Method Details

ClearConfigCache() public static méthode

Allows you to explicitly remove a config from the cache, for example during unit testing, or any time when you know the config file in the cache is no longer valid.
public static ClearConfigCache ( string appName ) : void
appName string Identifies which config file we want.
Résultat void

ComponentExists() public méthode

Method to check if a config section exists for a component, prior to calling GetConfigXml or GetParametersAsHashTable (which throw exceptions if you request an invalid component name).
public ComponentExists ( string component ) : bool
component string The component or section of the config file, used to /// locate the parameter.
Résultat bool

Config() public méthode

Constructs a config class given the "appName", or the key to look up in the app/web.config's "appSettings" section. The value for that key is the path to the config file we're interested in.
public Config ( string appName ) : System
appName string Identifies which config file we want.
Résultat System

Config() public méthode

Construct a config directly from an XML document rather than from a file.
public Config ( string appName, XmlDocument configXml ) : System
appName string App name (I.E. config file name or whatever), used for logging.
configXml System.Xml.XmlDocument The XML containing all the config information.
Résultat System

Config() public méthode

Constructs a config class given a specific config file to load.
public Config ( string configFileName, string appName ) : System
configFileName string The file name of the configuration file to load.
appName string Since the config file is specified, this app name is just /// used for identification in log/error messages.
Résultat System

Config() public méthode

Construct a config class. The app name is used for logging, and to get the config file name if the file name was not specified and the XML was not passed directly. If you provide the XML, the app name is just used for logging, and the filename is stored but not used for anything (and may be blank).
public Config ( string configFileName, string appName, XmlDocument configXml ) : System
configFileName string The file name of the configuration file to load.
appName string App name (I.E. config file name or whatever), used for logging.
configXml System.Xml.XmlDocument The XML containing all the config information.
Résultat System

Config() protected méthode

The default behavior of Config is to throw an exception if the config file does not exist. This constructor allows a child class to override that, in which case a missing file will be treated as though it were empty (no values loaded, but no exception thrown).
protected Config ( string configFileName, string appName, XmlDocument configXml, bool treatMissingFileAsEmpty ) : System
configFileName string The file name of the configuration file to load.
appName string App name (I.E. config file name or whatever), used for logging.
configXml System.Xml.XmlDocument The XML containing all the config information.
treatMissingFileAsEmpty bool If true, a missing config file will not cause an exception.
Résultat System

Config() protected méthode

This signature lets a child class provide more complicated or specific types of collections. Reminder: You probably want to use CaseInsensitiveStringComparers in your dictionaries!
protected Config ( string configFileName, string appName, XmlDocument configXml, bool treatMissingFileAsEmpty, string>.IDictionary paramsByComponent, string>.IDictionary orderedParamsByComponent, string>.IDictionary outerXmlByComponent, string>.IDictionary innerXmlByComponent ) : System
configFileName string The file name of the configuration file to load.
appName string App name (I.E. config file name or whatever), used for logging.
configXml System.Xml.XmlDocument The XML containing all the config information.
treatMissingFileAsEmpty bool If true, a missing config file will not cause an exception.
paramsByComponent string>.IDictionary The dictionary that will hold the parameters keyed by component.
orderedParamsByComponent string>.IDictionary The dictionary that will hold the parameters in order from the file, keyed by component.
outerXmlByComponent string>.IDictionary The dictionary that will hold XML chunks from the file, keyed by component.
innerXmlByComponent string>.IDictionary The dictionary that will hold XML chunks from the file, keyed by component.
Résultat System

Equals() public méthode

Two Configs with the same config file and appname are Equal.
The parameter is null.
public Equals ( object obj ) : bool
obj object The to compare with the current .
Résultat bool

GetConfig() public static méthode

This allows you to avoid reading the same config file over and over again. Since Config objects are read-only, we can read the file once and hand the same object out over and over without worrying about threading issues.
public static GetConfig ( string appName ) : Config
appName string Identifies which config file we want.
Résultat Config

GetConfigInnerXml() public méthode

Gets you the XML section for the component, allowing you to do any special parsing that may be necessary. This includes ONLY the children of the "component" tag, and will not have any text included in that tag.
public GetConfigInnerXml ( string component ) : string
component string The component or section of the config file, used to /// locate the parameter.
Résultat string

GetConfigXml() public méthode

Gets you the XML section for the component, allowing you to do any special parsing that may be necessary. This includes the "component" tag, and any text included in that tag.
public GetConfigXml ( string component ) : string
component string The component or section of the config file, used to /// locate the parameter.
Résultat string

GetHashCode() public méthode

Hash code, based on the config file name and app name.
public GetHashCode ( ) : int
Résultat int

GetParameter() public méthode

Returns the config parameter for the given component. Throws an exception if there is no such parameter. If you want to know if the parameter exists, call ParameterExists(...).
public GetParameter ( string component, string parameter ) : string
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
Résultat string

GetParameter() public méthode

Similar to GetParameter, except rather than throwing an exception if a parameter doesn't exist, returns the default value.
public GetParameter ( string component, string parameter, string defaultValue ) : string
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue string Value to return if the parameter doesn't exist.
Résultat string

GetParameterAsBool() public méthode

Similar to GetParameter, but converts the type for you (if possible, throws if not).
public GetParameterAsBool ( string component, string parameter ) : bool
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
Résultat bool

GetParameterAsBool() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsBool ( string component, string parameter, bool defaultValue ) : bool
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue bool Value to return if the parameter doesn't exist.
Résultat bool

GetParameterAsBool() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsBool ( string component, string parameter, bool defaultValue ) : bool?
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue bool Value to return if the parameter doesn't exist.
Résultat bool?

GetParameterAsDouble() public méthode

Similar to GetParameter, but converts the type for you (if possible, throws if not).
public GetParameterAsDouble ( string component, string parameter ) : double
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
Résultat double

GetParameterAsDouble() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsDouble ( string component, string parameter, double defaultValue ) : double
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue double Value to return if the parameter doesn't exist.
Résultat double

GetParameterAsDouble() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsDouble ( string component, string parameter, double defaultValue ) : double?
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue double Value to return if the parameter doesn't exist.
Résultat double?

GetParameterAsInt() public méthode

Similar to GetParameter, but converts the type for you (if possible, throws if not).
public GetParameterAsInt ( string component, string parameter ) : int
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
Résultat int

GetParameterAsInt() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsInt ( string component, string parameter, int defaultValue ) : int
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue int Value to return if the parameter doesn't exist.
Résultat int

GetParameterAsInt() public méthode

Similar to GetParameterWithDefault, except converts the type of the value (if present).
public GetParameterAsInt ( string component, string parameter, int defaultValue ) : int?
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
defaultValue int Value to return if the parameter doesn't exist.
Résultat int?

GetParameterWithSubstitution() public méthode

Similar to the regular GetParameter method, except it will substitute environment variables in the values if present.
public GetParameterWithSubstitution ( string component, string parameter, bool tolerant ) : string
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
tolerant bool If true, this method logs warnings for unmatched environment /// variables. If false, it throws exceptions.
Résultat string

GetParameterWithSubstitution() public méthode

Similar to the regular GetParameter method, except it will substitute environment variables in the values if present.
public GetParameterWithSubstitution ( string component, string parameter, bool tolerant, string defaultValue ) : string
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
tolerant bool If true, this method logs warnings for unmatched environment /// variables. If false, it throws exceptions.
defaultValue string Value to return if the parameter doesn't exist.
Résultat string

GetParametersAsDictionary() public méthode

Gets you a Dictionary of all the parameters for the component.
public GetParametersAsDictionary ( string component ) : string>.Dictionary
component string The component or section of the config file, used to /// locate the parameter.
Résultat string>.Dictionary

GetParametersAsList() public méthode

Gets you a list of all the parameters for the component as key-value-pairs. This preserves the order of parameters from the config file.
public GetParametersAsList ( string component ) : string>>.IList
component string The component or section of the config file, used to /// locate the parameter.
Résultat string>>.IList

MakeParameterCollection() protected méthode

This is a ugly hack at the moment. This allows child classes to override the internal type of collection we use. This will be removed when we refactor the architecture to have an abstract base class so we can have a "writeable" version of Config that does not conflict with the implementation of this "readonly" Config.
protected MakeParameterCollection ( ) : string>.IDictionary
Résultat string>.IDictionary

ParameterExists() public méthode

Method to check if a parameter exists, prior to calling GetParameter (which throws exceptions if you request an invalid parameter).
public ParameterExists ( string component, string parameter ) : bool
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
Résultat bool

ReThrowException() protected static méthode

All the checks for null are because there was an issue where something about a caught exception was null, which caused the error handling code to bomb. Since error handling code is the worst place to bomb (you lose the original exception), to be safe we manually convert null values into "null" strings.
protected static ReThrowException ( string msg, object paramList, Exception orig ) : void
msg string
paramList object
orig System.Exception
Résultat void

ReplaceEnvironmentVariables() public static méthode

If there are any environment variables (in the form %VAR%) in the input string, replaces them with the values from the environment. This method can be tolerant or intolerant of errors, so: "abc" -> "abc" "abc%windir%abc" -> "abcC:\WINDOWSabc" "abc%abc" -> exception (intolerant) or "abc%abc" (tolerant) "abc%nosuchvar%abc" -> exception (intolerant) or "abc%nosuchvar%abc" (tolerant) "abc%windir%abc%" -> exception (intolerant) or "abcC:\WINDOWSabc%" (tolerant) Calling this method with "false" for tolerant matches the previous behavior. Methods like File.Exists do not parse environment variables, so this method should be called before attempting to use filenames etc.
public static ReplaceEnvironmentVariables ( string val, bool tolerant ) : string
val string Input string to search for environment vars.
tolerant bool If true, this method logs warnings. If false, it /// throws exceptions.
Résultat string

ToString() public méthode

Returns the config file name and app name./>.
public ToString ( ) : string
Résultat string

Property Details

Application public_oe property

The app name passed in when constructing this object.
public string Application
Résultat string

ConfigFile public_oe property

The filename (including path) of the config file.
public string ConfigFile
Résultat string

ConfigXmlDoc public_oe property

The contents of the config file as an XmlDocument.
public XmlDocument,System.Xml ConfigXmlDoc
Résultat System.Xml.XmlDocument

_innerXmlByComponent protected_oe property

This is a Dictionary of the XML contents of each component/section, keyed by component (component = one section in the config file). The values are XML strings. This does NOT include the containing "component" tag.
protected IDictionary _innerXmlByComponent
Résultat string>.IDictionary

_log protected_oe static_oe property

A logger that can be used by child classes as well.
protected static ILog,log4net _log
Résultat log4net.ILog

_orderedParamsByComponent protected_oe property

This is a Dictionary of groups of parameters (key/value pairs), keyed by component (component = one section in the config file). The groups are lists, for times when the order of the parameters matters.
protected IDictionary>> _orderedParamsByComponent
Résultat IDictionary>>

_outerXmlByComponent protected_oe property

This is a Dictionary of the XML contents of each component/section, keyed by component (component = one section in the config file). The values are XML strings. This includes the containing "component" tag.
protected IDictionary _outerXmlByComponent
Résultat string>.IDictionary

_paramsByComponent protected_oe property

This is a Dictionary of groups of parameters (key/value pairs), keyed by component (component = one section in the config file). The groups are dictionaries to facilitate fast lookups by key.
protected IDictionary> _paramsByComponent
Résultat IDictionary>