diff --git a/Solution/build-bin/HLF.ContextConfig.dll b/Solution/build-bin/HLF.ContextConfig.dll index 40cbccb..92ebceb 100644 Binary files a/Solution/build-bin/HLF.ContextConfig.dll and b/Solution/build-bin/HLF.ContextConfig.dll differ diff --git a/Solution/build-bin/HLF.ContextConfig.pdb b/Solution/build-bin/HLF.ContextConfig.pdb index 1026821..b908ff3 100644 Binary files a/Solution/build-bin/HLF.ContextConfig.pdb and b/Solution/build-bin/HLF.ContextConfig.pdb differ diff --git a/Solution/build-config/ContextConfig.config b/Solution/build-config/ContextConfig.config index 836868d..e79fa11 100644 --- a/Solution/build-config/ContextConfig.config +++ b/Solution/build-config/ContextConfig.config @@ -1,53 +1,66 @@ - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Source/HLF.ContextConfig/ConfigSettings.cs b/Source/HLF.ContextConfig/ConfigSettings.cs index 1908661..a12def8 100644 --- a/Source/HLF.ContextConfig/ConfigSettings.cs +++ b/Source/HLF.ContextConfig/ConfigSettings.cs @@ -1,473 +1,836 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Configuration; -using System.Web; - -namespace HLF.ContextConfig -{ - /// - /// The 'ConfigSettings' class give you direct access to all the configured data using collections and dot(.) notation. - /// *Note: you will need to explicitly declare objects of the element types in order to use their properties. - /// example: - /// foreach (DomainElement Domain in ConfigSettings.Settings.Domains) - /// { string EnvironmentName = Domain.Environment;} - /// - public class ConfigSettings : ConfigurationSection - { - //Config File Object - private static string _configfile = "ContextConfig.config"; - private static ConfigSettings _Config; - private static Configuration config; - - /// - /// Represents all the ConfigSettings - /// - public static ConfigSettings Settings = GetSettings(); - - internal static ConfigSettings GetSettings() - { - ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); - fileMap.ExeConfigFilename = HttpContext.Current.Server.MapPath(string.Format("~/config/{0}", _configfile)); - - if (! System.IO.File.Exists(fileMap.ExeConfigFilename)) - { - //if file doesn't exist in /config/ folder, check in site root. - fileMap.ExeConfigFilename = HttpContext.Current.Server.MapPath(string.Format("~/{0}", _configfile)); - } - - if (System.IO.File.Exists(fileMap.ExeConfigFilename)) - { - // load the settings file - config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); - - if (config != null) - { - _Config = (ConfigSettings)config.GetSection("ContextConfig"); - } - } - else - { - string ErrorMsg = string.Format("The 'ContextConfig.config' file cannot be found. Please add it to the '~/config/' folder or site root."); - throw new MissingConfigFileException(ErrorMsg); - } - - if (_Config == null) - { - string ErrorMsg = string.Format("There is something wrong with the 'ContextConfig.config' file. Please check that a properly formatted file is located in the '~/config/' folder or site root."); - throw new MissingConfigFileException(ErrorMsg); - //_Config = new ConfigSettings(); // default config - won't be savable mind? - } - - return _Config; - } - - #region *** ConfigurationProperties *** - - /// - /// <ContextConfig> 'version' attribute - /// - [ConfigurationProperty("version")] - public string Version - { - get { return (string)base["version"]; } - } - - /// - /// <ContextConfig> 'OverrideConfigurationManager' attribute - /// - [ConfigurationProperty("OverrideConfigurationManager")] - public bool OverrideConfigurationManager - { - get - { - //string ConfigValue = (string)base["OverrideConfigurationManager"]; - //return Convert.ToBoolean(ConfigValue); - return (bool)base["OverrideConfigurationManager"]; - } - } - - /// - /// <ContextConfig> <Domains> collection - /// - [ConfigurationProperty("Domains")] - public DomainElementCollection Domains - { - get { return (DomainElementCollection) base["Domains"]; } - } - - /// - /// <ContextConfig> <Environments> collection - /// - [ConfigurationProperty("Environments")] - public EnvironmentElementCollection Environments - { - get { return (EnvironmentElementCollection) base["Environments"]; } - } - - #endregion - - } - - #region *** Element Objects *** - - /// - /// Represents a Domain from the config file - /// - public class DomainElement : ConfigurationElement - { - /// - /// <Domain> 'url' attribute - /// - [ConfigurationProperty("url", IsRequired = true)] - public string Url - { - get { return (string) base["url"]; } - } - - /// - /// <Domain> 'environment' attribute - /// - [ConfigurationProperty("environment", IsRequired = true)] - public string Environment - { - get { return (string) base["environment"]; } - } - - /// - /// <Domain> 'sitename' attribute - /// - [ConfigurationProperty("sitename", IsRequired = false)] - public string SiteName - { - get { return (string)base["sitename"]; } - } - } - - /// - /// Represents a defined Environment from the config file - /// - public class EnvironmentElement : ConfigurationElement - { - - /// - /// <Environment> 'name' attribute - /// - [ConfigurationProperty("name", IsRequired = true)] - public string Name - { - get { return (string) base["name"]; } - } - - - /// - /// <Environment> <Configs> (key/values) collection - /// - [ConfigurationProperty("Configs")] - public KeyValueElementCollection Configs - { - get { return (KeyValueElementCollection) base["Configs"]; } - } - - } - - /// - /// Represents a Key/Value pair defined for an environment in the config file - /// - public class KeyValueElement : ConfigurationElement - { - - /// - /// <Environment> <add> 'key' attribute - /// - [ConfigurationProperty("key", IsRequired = true)] - public string Key - { - get { return (string) base["key"]; } - } - - /// - /// <Environment> <add>'value' attribute - /// - [ConfigurationProperty("value", IsRequired = false)] - public string Value - { - get { return (string) base["value"]; } - } - } - - #endregion - - #region *** Element Collections *** - - /// - /// Represents all the Domains defined in the config file - /// - [ConfigurationCollection(typeof (DomainElement), AddItemName = "Domain", - CollectionType = ConfigurationElementCollectionType.BasicMap)] - public class DomainElementCollection : ConfigurationElementCollection - { - /// - /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. - /// - /// - /// The name of the collection; otherwise, an empty string. The default is an empty string. - /// - protected override string ElementName - { - get { return "Domain"; } - } - - //Basic Stuff - /// - /// Gets the type of the . - /// - /// - /// The of this collection. - /// - public override ConfigurationElementCollectionType CollectionType - { - get { return ConfigurationElementCollectionType.BasicMap; } - } - - /// - /// When overridden in a derived class, creates a new . - /// - /// - /// A new . - /// - protected override ConfigurationElement CreateNewElement() - { - return new DomainElement(); - } - - /// - /// Integer index - /// - /// - public DomainElement this[int index] - { - get { return (DomainElement) base.BaseGet(index); } - set - { - if (base.BaseGet(index) != null) - { - base.BaseRemoveAt(index); - } - base.BaseAdd(index, value); - } - } - - //Custom Key - /// - /// Gets the element key for a specified configuration element when overridden in a derived class. - /// - /// - /// An that acts as the key for the specified . - /// - /// The to return the key for. - protected override object GetElementKey(ConfigurationElement element) - { - return (element as DomainElement).Url; - } - - /// - /// - /// - /// - public new DomainElement this[string url] - { - get { return (DomainElement) base.BaseGet(url); } - } - } - - /// - /// Represents all the Environments defined in the config file - /// - [ConfigurationCollection(typeof (EnvironmentElement), AddItemName = "Environment", - CollectionType = ConfigurationElementCollectionType.BasicMap)] - public class EnvironmentElementCollection : ConfigurationElementCollection - { - /// - /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. - /// - /// - /// The name of the collection; otherwise, an empty string. The default is an empty string. - /// - protected override string ElementName - { - get { return "Environment"; } - } - - //Basic Stuff - /// - /// Gets the type of the . - /// - /// - /// The of this collection. - /// - public override ConfigurationElementCollectionType CollectionType - { - get { return ConfigurationElementCollectionType.BasicMap; } - } - - /// - /// When overridden in a derived class, creates a new . - /// - /// - /// A new . - /// - protected override ConfigurationElement CreateNewElement() - { - return new EnvironmentElement(); - } - - /// - /// - /// - /// - public EnvironmentElement this[int index] - { - get { return (EnvironmentElement) base.BaseGet(index); } - set - { - if (base.BaseGet(index) != null) - { - base.BaseRemoveAt(index); - } - base.BaseAdd(index, value); - } - } - - //Custom Key - /// - /// Gets the element key for a specified configuration element when overridden in a derived class. - /// - /// - /// An that acts as the key for the specified . - /// - /// The to return the key for. - protected override object GetElementKey(ConfigurationElement element) - { - return (element as EnvironmentElement).Name; - } - - /// - /// - /// - /// - public new EnvironmentElement this[string name] - { - get { return (EnvironmentElement) base.BaseGet(name); } - } - } - - /// - /// Represents all the Key/Value pair elements defined in the config file - /// - [ConfigurationCollection(typeof (KeyValueElement), AddItemName = "add", - CollectionType = ConfigurationElementCollectionType.BasicMap)] - public class KeyValueElementCollection : ConfigurationElementCollection - { - /// - /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. - /// - /// - /// The name of the collection; otherwise, an empty string. The default is an empty string. - /// - protected override string ElementName - { - get { return "add"; } - } - - //Basic Stuff - /// - /// Gets the type of the . - /// - /// - /// The of this collection. - /// - public override ConfigurationElementCollectionType CollectionType - { - get { return ConfigurationElementCollectionType.BasicMap; } - } - - /// - /// When overridden in a derived class, creates a new . - /// - /// - /// A new . - /// - protected override ConfigurationElement CreateNewElement() - { - return new KeyValueElement(); - } - - /// - /// - /// - /// - public KeyValueElement this[int index] - { - get { return (KeyValueElement) base.BaseGet(index); } - set - { - if (base.BaseGet(index) != null) - { - base.BaseRemoveAt(index); - } - base.BaseAdd(index, value); - } - } - - //Custom Key - /// - /// Gets the element key for a specified configuration element when overridden in a derived class. - /// - /// - /// An that acts as the key for the specified . - /// - /// The to return the key for. - protected override object GetElementKey(ConfigurationElement element) - { - return (element as KeyValueElement).Key; - } - - /// - /// - /// - /// - public new KeyValueElement this[string key] - { - get { return (KeyValueElement) base.BaseGet(key); } - } - } - - #endregion - - #region *** Custom Exceptions *** - - [Serializable] - internal class MissingConfigFileException : Exception - { - // Use the default ApplicationException constructors - public MissingConfigFileException() - : base() - { - } - - public MissingConfigFileException(string s) - : base(s) - { - } - - public MissingConfigFileException(string s, Exception ex) - : base(s, ex) - { - } - } - - #endregion -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Configuration; +using System.Web; + +namespace HLF.ContextConfig +{ + /// + /// The 'ConfigSettings' class give you direct access to all the configured data using collections and dot(.) notation. + /// *Note: you will need to explicitly declare objects of the element types in order to use their properties. + /// example: + /// foreach (DomainElement Domain in ConfigSettings.Settings.Domains) + /// { string EnvironmentName = Domain.Environment;} + /// + public class ConfigSettings : ConfigurationSection + { + //Config File Object + private static string _configfile = "ContextConfig.config"; + private static ConfigSettings _Config; + private static Configuration config; + + /// + /// Represents all the ConfigSettings + /// + public static ConfigSettings Settings = GetSettings(); + + internal static ConfigSettings GetSettings() + { + ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); + fileMap.ExeConfigFilename = HttpContext.Current.Server.MapPath(string.Format("~/config/{0}", _configfile)); + + if (! System.IO.File.Exists(fileMap.ExeConfigFilename)) + { + //if file doesn't exist in /config/ folder, check in site root. + fileMap.ExeConfigFilename = HttpContext.Current.Server.MapPath(string.Format("~/{0}", _configfile)); + } + + if (System.IO.File.Exists(fileMap.ExeConfigFilename)) + { + // load the settings file + config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); + + if (config != null) + { + _Config = (ConfigSettings)config.GetSection("ContextConfig"); + } + } + else + { + string ErrorMsg = string.Format("The 'ContextConfig.config' file cannot be found. Please add it to the '~/config/' folder or site root."); + throw new MissingConfigFileException(ErrorMsg); + } + + if (_Config == null) + { + string ErrorMsg = string.Format("There is something wrong with the 'ContextConfig.config' file. Please check that a properly formatted file is located in the '~/config/' folder or site root."); + throw new MissingConfigFileException(ErrorMsg); + //_Config = new ConfigSettings(); // default config - won't be savable mind? + } + + return _Config; + } + + #region *** ConfigurationProperties *** + + /// + /// <ContextConfig> 'version' attribute + /// + [ConfigurationProperty("version")] + public string Version + { + get { return (string)base["version"]; } + } + + /// + /// <ContextConfig> 'OverrideConfigurationManager' attribute + /// + [ConfigurationProperty("OverrideConfigurationManager")] + public bool OverrideConfigurationManager + { + get + { + //string ConfigValue = (string)base["OverrideConfigurationManager"]; + //return Convert.ToBoolean(ConfigValue); + return (bool)base["OverrideConfigurationManager"]; + } + } + + /// + /// <ContextConfig> <Domains> collection + /// + [ConfigurationProperty("Domains")] + public DomainElementCollection Domains + { + get { return (DomainElementCollection) base["Domains"]; } + } + + /// + /// <ContextConfig> <Environments> collection + /// + [ConfigurationProperty("Environments")] + public EnvironmentElementCollection Environments + { + get { return (EnvironmentElementCollection) base["Environments"]; } + } + + #endregion + + } + + #region *** Element Objects *** + + /// + /// Represents a Domain from the config file + /// + public class DomainElement : ConfigurationElement + { + /// + /// <Domain> 'url' attribute + /// + [ConfigurationProperty("url", IsRequired = true)] + public string Url + { + get { return (string) base["url"]; } + } + + /// + /// <Domain> 'environment' attribute + /// + [ConfigurationProperty("environment", IsRequired = true)] + public string Environment + { + get { return (string) base["environment"]; } + } + + /// + /// <Domain> 'sitename' attribute + /// + [ConfigurationProperty("sitename", IsRequired = false)] + public string SiteName + { + get { return (string)base["sitename"]; } + } + } + + /// + /// Represents a defined Environment from the config file + /// + public class EnvironmentElement : ConfigurationElement + { + + /// + /// <Environment> 'name' attribute + /// + [ConfigurationProperty("name", IsRequired = true)] + public string Name + { + get { return (string) base["name"]; } + } + + /// + /// <Environment> <Configs> (key/values) collection + /// + [ConfigurationProperty("Configs")] + public KeyValueElementCollection Configs + { + get { return (KeyValueElementCollection) base["Configs"]; } + } + + /// + /// <Environment> <SmtpSettings> element + /// + [ConfigurationProperty("SmtpSettings", IsRequired=false)] + public SmtpSettingsElement SmtpSettings + { + get { return (SmtpSettingsElement)base["SmtpSettings"]; } + } + } + + /// + /// Represents a Key/Value pair defined for an environment in the config file + /// + public class KeyValueElement : ConfigurationElement + { + + /// + /// <Environment> <add> 'key' attribute + /// + [ConfigurationProperty("key", IsRequired = true)] + public string Key + { + get { return (string) base["key"]; } + } + + /// + /// <Environment> <add>'value' attribute + /// + [ConfigurationProperty("value", IsRequired = false)] + public string Value + { + get { return (string) base["value"]; } + } + } + + public abstract class SmtpConfigurationElement : ConfigurationElement + { + System.Net.Mail.SmtpClient _smtpDefaults; + + /// + /// Returns an smtp client object. Helper object used to return default smtp config values in case they are + /// not provided in this section. + /// + protected System.Net.Mail.SmtpClient SmtpDefaults + { + get + { + if (_smtpDefaults == null) + _smtpDefaults = new System.Net.Mail.SmtpClient(); + return _smtpDefaults; + } + } + + /// + /// Checks whether a specific configuration property was provided or was absent from the configuration element + /// + /// Name of the property to check on + /// + internal bool IsPropertyUndefined(string PropertyName) + { + var property = this.ElementInformation.Properties[PropertyName]; + + return property == null || property.ValueOrigin != PropertyValueOrigin.SetHere; + } + } + + /// + /// Represents the SmtpSettings defined for an environment in the config file + /// + public class SmtpSettingsElement : SmtpConfigurationElement + { + #region const + internal const string DeliveryMethodPropertyName = "deliveryMethod", DeliveryFormatPropertyName = "deliveryFormat", FromPropertyName = "from"; + protected const string NetworkPropertyName = "network", SpecifiedPickupDirectoryPropertyName = "specifiedPickupDirectory"; + #endregion + + + /// + /// <SmtpSettings> 'deliveryMethod' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(DeliveryMethodPropertyName, IsRequired = false)] + public System.Net.Mail.SmtpDeliveryMethod DeliveryMethod + { + get + { + if (IsPropertyUndefined(DeliveryMethodPropertyName)) + return SmtpDefaults.DeliveryMethod; // return default value + + return (System.Net.Mail.SmtpDeliveryMethod)base[DeliveryMethodPropertyName]; + } + set + { + base[DeliveryMethodPropertyName] = value; + } + } + + /// + /// <SmtpSettings> 'deliveryFormat' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(DeliveryFormatPropertyName, IsRequired = false)] + public System.Net.Mail.SmtpDeliveryFormat DeliveryFormat + { + get + { + if (IsPropertyUndefined(DeliveryFormatPropertyName)) + return SmtpDefaults.DeliveryFormat; // return default value + + return (System.Net.Mail.SmtpDeliveryFormat)base[DeliveryFormatPropertyName]; + } + set + { + base[DeliveryFormatPropertyName] = value; + } + } + + /// + /// <SmtpSettings> 'from' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(FromPropertyName, IsRequired = false)] + public string From + { + get + { + if (IsPropertyUndefined(FromPropertyName)) + { + // return default value, if provided + var defaultFrom = new System.Net.Mail.MailMessage().From; + if (defaultFrom != null) + return defaultFrom.Address; // we have a default from address + } + + return (string)base[FromPropertyName]; + } + set + { + base[FromPropertyName] = value; + } + } + + /// + /// <SmtpSettings> 'network' element. + /// Defaults to the base smtp network settings value if not provided in this config section. + /// + [ConfigurationProperty(NetworkPropertyName, IsRequired = false)] + public SmtpNetworkElement Network + { + get + { + return (SmtpNetworkElement)base[NetworkPropertyName]; + } + } + + /// + /// <SmtpSettings> 'specifiedPickupDirectory' element. + /// Defaults to the base smtp specifiedPickupDirectory settings value if not provided in this config section. + /// + [ConfigurationProperty(SpecifiedPickupDirectoryPropertyName, IsRequired = false)] + public SmtpSpecifiedPickupDirectoryElement SpecifiedPickupDirectory + { + get { return (SmtpSpecifiedPickupDirectoryElement)base[SpecifiedPickupDirectoryPropertyName]; } + } + } + + /// + /// Represents the Network configuration for SmtpSettings defined for an environment in the config file + /// + public class SmtpNetworkElement : SmtpConfigurationElement + { + #region const + internal const string ClientDomainPropertyName = "clientDomain", UserNamePropertyName = "userName", PasswordPropertyName = "password", + DefaultCredentialsPropertyName = "defaultCredentials", EnableSslPropertyName = "enableSsl", HostPropertyName = "host", + PortPropertyName = "port", TargetNamePropertyName = "targetName"; + #endregion + + /// + /// <Network> 'clientDomain' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(ClientDomainPropertyName, IsRequired = false)] + public string ClientDomain + { + get + { + if (UseDefaultCredentials) + return string.Empty; + + if (IsPropertyUndefined(ClientDomainPropertyName)) + { + var defaultCredentials = (System.Net.NetworkCredential)SmtpDefaults.Credentials; + + if (defaultCredentials != null) + return defaultCredentials.Domain; + } + + return (string)base[ClientDomainPropertyName]; + } + set + { + base[ClientDomainPropertyName] = value; + } + } + + /// + /// <Network> 'userName' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(UserNamePropertyName, IsRequired = false)] + public string UserName + { + get + { + if (UseDefaultCredentials) + return string.Empty; + + if (IsPropertyUndefined(UserNamePropertyName)) + { + var defaultCredentials = (System.Net.NetworkCredential)SmtpDefaults.Credentials; + + if (defaultCredentials != null) + return defaultCredentials.UserName; + } + + return (string)base[UserNamePropertyName]; + } + set + { + base[UserNamePropertyName] = value; + } + } + + /// + /// <Network> 'password' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(PasswordPropertyName, IsRequired = false)] + public string Password + { + get + { + if (UseDefaultCredentials) + return string.Empty; + + if (IsPropertyUndefined(PasswordPropertyName)) + { + var defaultCredentials = (System.Net.NetworkCredential)SmtpDefaults.Credentials; + + if (defaultCredentials != null) + return defaultCredentials.Password; + } + + return (string)base[PasswordPropertyName]; + } + set + { + base[PasswordPropertyName] = value; + } + } + + /// + /// <Network> 'defaultCredentials' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(DefaultCredentialsPropertyName, IsRequired = false)] + public bool UseDefaultCredentials + { + get + { + if (IsPropertyUndefined(DefaultCredentialsPropertyName)) + return SmtpDefaults.UseDefaultCredentials; + + return (bool)base[DefaultCredentialsPropertyName]; + } + set + { + base[DefaultCredentialsPropertyName] = value; + } + } + + /// + /// <Network> 'enableSsl' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(EnableSslPropertyName, IsRequired = false)] + public bool EnableSsl + { + get + { + if (IsPropertyUndefined(EnableSslPropertyName)) + return SmtpDefaults.EnableSsl; + + return (bool)base[EnableSslPropertyName]; + } + set + { + base[EnableSslPropertyName] = value; + } + } + + /// + /// <Network> 'host' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(HostPropertyName, IsRequired = false)] + public string Host + { + get + { + if (IsPropertyUndefined(HostPropertyName)) + return SmtpDefaults.Host; + + return (string)base[HostPropertyName]; + } + set + { + base[HostPropertyName] = value; + } + } + + /// + /// <Network> 'port' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(PortPropertyName, IsRequired = false)] + public int Port + { + get + { + if (IsPropertyUndefined(PortPropertyName)) + return SmtpDefaults.Port; + + return (int)base[PortPropertyName]; + } + set + { + base[PortPropertyName] = value; + } + } + + /// + /// <Network> 'targetName' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(TargetNamePropertyName, IsRequired = false)] + public string TargetName + { + get + { + if (IsPropertyUndefined(TargetNamePropertyName)) + return SmtpDefaults.TargetName; + + return (string)base[TargetNamePropertyName]; + } + set + { + base[TargetNamePropertyName] = value; + } + } + } + + /// + /// Represents the SpecifiedPickupDirectory configuration for SmtpSettings defined for an environment in the config file + /// + public class SmtpSpecifiedPickupDirectoryElement : SmtpConfigurationElement + { + #region const + internal const string PickupDirectoryLocationPropertyName = "pickupDirectoryLocation"; + #endregion + + /// + /// <SpecifiedPickupDirectory> 'pickupDirectoryLocation' attribute. + /// Defaults to the base smtp settings value if not provided in this config section. + /// + [ConfigurationProperty(PickupDirectoryLocationPropertyName, IsRequired = false)] + public string PickupDirectoryLocation + { + get + { + if (IsPropertyUndefined(PickupDirectoryLocationPropertyName)) + return SmtpDefaults.PickupDirectoryLocation; + + return (string)base[PickupDirectoryLocationPropertyName]; + } + set + { + base[PickupDirectoryLocationPropertyName] = value; + } + } + } + #endregion + + #region *** Element Collections *** + + /// + /// Represents all the Domains defined in the config file + /// + [ConfigurationCollection(typeof (DomainElement), AddItemName = "Domain", + CollectionType = ConfigurationElementCollectionType.BasicMap)] + public class DomainElementCollection : ConfigurationElementCollection + { + /// + /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. + /// + /// + /// The name of the collection; otherwise, an empty string. The default is an empty string. + /// + protected override string ElementName + { + get { return "Domain"; } + } + + //Basic Stuff + /// + /// Gets the type of the . + /// + /// + /// The of this collection. + /// + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + /// + /// When overridden in a derived class, creates a new . + /// + /// + /// A new . + /// + protected override ConfigurationElement CreateNewElement() + { + return new DomainElement(); + } + + /// + /// Integer index + /// + /// + public DomainElement this[int index] + { + get { return (DomainElement) base.BaseGet(index); } + set + { + if (base.BaseGet(index) != null) + { + base.BaseRemoveAt(index); + } + base.BaseAdd(index, value); + } + } + + //Custom Key + /// + /// Gets the element key for a specified configuration element when overridden in a derived class. + /// + /// + /// An that acts as the key for the specified . + /// + /// The to return the key for. + protected override object GetElementKey(ConfigurationElement element) + { + return (element as DomainElement).Url; + } + + /// + /// + /// + /// + public new DomainElement this[string url] + { + get { return (DomainElement) base.BaseGet(url); } + } + } + + /// + /// Represents all the Environments defined in the config file + /// + [ConfigurationCollection(typeof (EnvironmentElement), AddItemName = "Environment", + CollectionType = ConfigurationElementCollectionType.BasicMap)] + public class EnvironmentElementCollection : ConfigurationElementCollection + { + /// + /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. + /// + /// + /// The name of the collection; otherwise, an empty string. The default is an empty string. + /// + protected override string ElementName + { + get { return "Environment"; } + } + + //Basic Stuff + /// + /// Gets the type of the . + /// + /// + /// The of this collection. + /// + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + /// + /// When overridden in a derived class, creates a new . + /// + /// + /// A new . + /// + protected override ConfigurationElement CreateNewElement() + { + return new EnvironmentElement(); + } + + /// + /// + /// + /// + public EnvironmentElement this[int index] + { + get { return (EnvironmentElement) base.BaseGet(index); } + set + { + if (base.BaseGet(index) != null) + { + base.BaseRemoveAt(index); + } + base.BaseAdd(index, value); + } + } + + //Custom Key + /// + /// Gets the element key for a specified configuration element when overridden in a derived class. + /// + /// + /// An that acts as the key for the specified . + /// + /// The to return the key for. + protected override object GetElementKey(ConfigurationElement element) + { + return (element as EnvironmentElement).Name; + } + + /// + /// + /// + /// + public new EnvironmentElement this[string name] + { + get { return (EnvironmentElement) base.BaseGet(name); } + } + } + + /// + /// Represents all the Key/Value pair elements defined in the config file + /// + [ConfigurationCollection(typeof (KeyValueElement), AddItemName = "add", + CollectionType = ConfigurationElementCollectionType.BasicMap)] + public class KeyValueElementCollection : ConfigurationElementCollection + { + /// + /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. + /// + /// + /// The name of the collection; otherwise, an empty string. The default is an empty string. + /// + protected override string ElementName + { + get { return "add"; } + } + + //Basic Stuff + /// + /// Gets the type of the . + /// + /// + /// The of this collection. + /// + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + /// + /// When overridden in a derived class, creates a new . + /// + /// + /// A new . + /// + protected override ConfigurationElement CreateNewElement() + { + return new KeyValueElement(); + } + + /// + /// + /// + /// + public KeyValueElement this[int index] + { + get { return (KeyValueElement) base.BaseGet(index); } + set + { + if (base.BaseGet(index) != null) + { + base.BaseRemoveAt(index); + } + base.BaseAdd(index, value); + } + } + + //Custom Key + /// + /// Gets the element key for a specified configuration element when overridden in a derived class. + /// + /// + /// An that acts as the key for the specified . + /// + /// The to return the key for. + protected override object GetElementKey(ConfigurationElement element) + { + return (element as KeyValueElement).Key; + } + + /// + /// + /// + /// + public new KeyValueElement this[string key] + { + get { return (KeyValueElement) base.BaseGet(key); } + } + } + + #endregion + + #region *** Custom Exceptions *** + + [Serializable] + internal class MissingConfigFileException : Exception + { + // Use the default ApplicationException constructors + public MissingConfigFileException() + : base() + { + } + + public MissingConfigFileException(string s) + : base(s) + { + } + + public MissingConfigFileException(string s, Exception ex) + : base(s, ex) + { + } + } + + #endregion +} diff --git a/Source/HLF.ContextConfig/ContextConfig.config b/Source/HLF.ContextConfig/ContextConfig.config index 836868d..e79fa11 100644 --- a/Source/HLF.ContextConfig/ContextConfig.config +++ b/Source/HLF.ContextConfig/ContextConfig.config @@ -1,53 +1,66 @@ - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Source/HLF.ContextConfig/ContextConfig.cs b/Source/HLF.ContextConfig/ContextConfig.cs index 911b380..2fc0c29 100644 --- a/Source/HLF.ContextConfig/ContextConfig.cs +++ b/Source/HLF.ContextConfig/ContextConfig.cs @@ -1,436 +1,517 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Web; - -namespace HLF.ContextConfig -{ - /// - /// The 'ContextConfig' static class includes useful functions to test and return data about domains, environments, and configured key/value pairs. - /// - public static class ContextConfig - { - - #region Domain Info - - /// - /// Current active domain url - /// - public static string CurrentDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"].ToString(); - - /// - /// Check whether the current domain exists in the Domains list - /// - /// If there is a wildcard (*) domain specified, return true? (Choose false to explicitly search for this url) - /// - public static bool DomainIsConfigured(bool AcceptWildcard = true) - { - return DomainIsConfigured(CurrentDomain, AcceptWildcard); - } - - /// - /// Check whether the URL exists in the Domains list - /// - /// Url to lookup - /// If there is a wildcard (*) domain specified, return true? (Choose false to explicitly search for this url) - /// - public static bool DomainIsConfigured(string DomainUrl, bool AcceptWildcard = true) - { - bool ReturnValue = false; - - //Try to get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; - - if (Domain != null) - { - ReturnValue = true; - } - else - { - if (AcceptWildcard) - { - DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; - if (DomainWild != null) - { - ReturnValue = true; - } - } - } - - return ReturnValue; - } - - /// - /// Get the Environment name for the current domain - /// - /// - public static string DomainEnvironmentName() - { - string ReturnValue = ""; - - //Get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[CurrentDomain]; - - ReturnValue = Domain.Environment; - - return ReturnValue; - } - - /// - /// Get the Environment name for the provided domain url - /// - /// Url to lookup - /// - public static string DomainEnvironmentName(string DomainUrl) - { - string ReturnValue = ""; - - //Get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; - - if (Domain != null) - { - ReturnValue = Domain.Environment; - } - else - { - //look for wildcard domain - DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; - if (DomainWild != null) - { - ReturnValue = DomainWild.Environment; - } - else - { - string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); - throw new MissingDomainConfigException(ErrorMsg); - } - } - - return ReturnValue; - } - - /// - /// Get the Site Name for the current domain - /// - /// - public static string DomainSiteName() - { - string ReturnValue = ""; - - //Get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[CurrentDomain]; - - ReturnValue = Domain.SiteName; - - return ReturnValue; - } - - /// - /// Get the Site Name for the provided domain url - /// - /// Url to lookup - /// - public static string DomainSiteName(string DomainUrl) - { - string ReturnValue = ""; - - //Get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; - - if (Domain != null) - { - ReturnValue = Domain.SiteName; - } - else - { - //look for wildcard domain - DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; - if (DomainWild != null) - { - ReturnValue = DomainWild.SiteName; - } - else - { - string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); - throw new MissingDomainConfigException(ErrorMsg); - } - } - - return ReturnValue; - } - - #endregion - - #region Environment Info - - /// - /// Check whether the current environment exists in the Environments list - /// - /// If there is a "default" domain specified, return true? (Choose false to explicitly search for this environment) - /// - public static bool EnvironmentIsConfigured(bool AcceptDefault = true) - { - return EnvironmentIsConfigured(DomainEnvironmentName(), AcceptDefault); - } - - /// - /// Check whether the current environment exists in the Environments list - /// - /// Name to lookup - /// If there is a "default" domain specified, return true? (Choose false to explicitly search for this environment) - /// - public static bool EnvironmentIsConfigured(string EnvironmentName, bool AcceptDefault = true) - { - bool ReturnValue = false; - - //Try to get the environment from config file - EnvironmentElement Env = ConfigSettings.Settings.Environments[EnvironmentName]; - - if (Env != null) - { - ReturnValue = true; - } - else - { - if (AcceptDefault) - { - EnvironmentElement EnvDefault = ConfigSettings.Settings.Environments["default"]; - if (EnvDefault != null) - { - ReturnValue = true; - } - } - } - - return ReturnValue; - } - - internal static EnvironmentElement GetDomainEnvironment(string DomainUrl) - { - string EnvName = ""; - try - { - //Get domain from config file - DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; - EnvName = Domain.Environment; - } - catch (Exception ) - { - try - { - DomainElement Domain = ConfigSettings.Settings.Domains["*"]; - EnvName = Domain.Environment; - } - catch (Exception Exception2) - { - string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); - throw new MissingDomainConfigException(ErrorMsg, Exception2); - } - } - - if (EnvName != "") - { - EnvironmentElement ReturnValue = ConfigSettings.Settings.Environments[EnvName]; - return ReturnValue; - } - else - { - return null; - } - } - - internal static EnvironmentElement GetEnvironmentByName(string EnvironmentName) - { - EnvironmentElement ReturnValue; - - try - { - //Get environment from config file - ReturnValue = ConfigSettings.Settings.Environments[EnvironmentName]; - } - catch (Exception ) - { - try - { - //look for default environment - ReturnValue = ConfigSettings.Settings.Environments["default"]; - } - catch (Exception Exception2) - { - string ErrorMsg = string.Format("Environment '{0}' is not configured, and there is no 'default' environment configured", EnvironmentName); - throw new MissingEnvironmentConfigException(ErrorMsg, Exception2); - } - } - - return ReturnValue; - } - - #endregion - - #region Key/Value Configs - - /// - /// Get the value for a given key on the current domain - /// - /// Key name - /// - public static string GetValue(string ConfigKey) - { - string ReturnValue = ""; - ReturnValue = GetValue(ConfigKey, DomainEnvironmentName()); - - return ReturnValue; - } - - /// - /// Get the value when providing a key and environment name - /// - /// Key name - /// Environment name - /// - public static string GetValue(string ConfigKey, string EnvironmentName) - { - string ReturnValue = ""; - - //Get environment from config file - EnvironmentElement Env = GetEnvironmentByName(EnvironmentName); - - try - { - //find matching key - KeyValueElement KeyValue = Env.Configs[ConfigKey]; - - if (KeyValue != null) - { - ReturnValue = KeyValue.Value; - } - else - { - //Look for 'default' environment - EnvironmentElement EnvDefault = GetEnvironmentByName("default"); - - //find matching key - KeyValueElement KeyValueDefault = EnvDefault.Configs[ConfigKey]; - - ReturnValue = KeyValueDefault.Value; - } - } - catch (Exception Exception1) - { - string ErrorMsg = string.Format("Key '{0}' is not configured for Environment '{1}', and there is no default key configured", ConfigKey, EnvironmentName); - throw new MissingConfigKeyException(ErrorMsg, Exception1); - } - - return ReturnValue; - } - - - /// - /// Get all the KeyValue elements for the current Environment - /// - /// Include all ‘default’ KeyValue configs for keys not specifically defined for the environment. - /// - public static List AllEnvironmentConfigs(bool IncludeDefaults = true) - { - return AllEnvironmentConfigs(DomainEnvironmentName(), IncludeDefaults); - } - - /// - /// Get all the KeyValue elements for the current Environment - /// - /// Environment to get values from - /// Include all ‘default’ KeyValue configs for keys not specifically defined for the environment. - /// - public static List AllEnvironmentConfigs(string EnvironmentName, bool IncludeDefaults = true) - { - List ReturnList = new List(); - - EnvironmentElement Env = ConfigSettings.Settings.Environments[EnvironmentName]; - KeyValueElementCollection KvColl = Env.Configs; - - foreach (KeyValueElement KeyValue in Env.Configs) - { - ReturnList.Add(KeyValue); - } - - if (IncludeDefaults) - { - EnvironmentElement EnvDefault = ConfigSettings.Settings.Environments["default"]; - - foreach (KeyValueElement KeyValueDef in EnvDefault.Configs) - { - if (KvColl[KeyValueDef.Key]==null) - { - ReturnList.Add(KeyValueDef); - } - } - } - - return ReturnList; - } - - #endregion - - } - - #region *** Custom Exceptions *** - - [Serializable] - internal class MissingDomainConfigException : Exception - { - // Use the default ApplicationException constructors - public MissingDomainConfigException() : base() - { - } - - public MissingDomainConfigException(string s) : base(s) - { - } - - public MissingDomainConfigException(string s, Exception ex) : base(s, ex) - { - } - } - - [Serializable] - internal class MissingEnvironmentConfigException : Exception - { - // Use the default ApplicationException constructors - public MissingEnvironmentConfigException() : base() - { - } - - public MissingEnvironmentConfigException(string s) : base(s) - { - } - - public MissingEnvironmentConfigException(string s, Exception ex) : base(s, ex) - { - } - } - - [Serializable] - internal class MissingConfigKeyException : Exception - { - // Use the default ApplicationException constructors - public MissingConfigKeyException() : base() - { - } - - public MissingConfigKeyException(string s) : base(s) - { - } - - public MissingConfigKeyException(string s, Exception ex) : base(s, ex) - { - } - } - - #endregion - -} - +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Web; + +namespace HLF.ContextConfig +{ + /// + /// The 'ContextConfig' static class includes useful functions to test and return data about domains, environments, and configured key/value pairs. + /// + public static class ContextConfig + { + + #region Domain Info + + /// + /// Current active domain url + /// + public static string CurrentDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"].ToString(); + + /// + /// Check whether the current domain exists in the Domains list + /// + /// If there is a wildcard (*) domain specified, return true? (Choose false to explicitly search for this url) + /// + public static bool DomainIsConfigured(bool AcceptWildcard = true) + { + return DomainIsConfigured(CurrentDomain, AcceptWildcard); + } + + /// + /// Check whether the URL exists in the Domains list + /// + /// Url to lookup + /// If there is a wildcard (*) domain specified, return true? (Choose false to explicitly search for this url) + /// + public static bool DomainIsConfigured(string DomainUrl, bool AcceptWildcard = true) + { + bool ReturnValue = false; + + //Try to get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; + + if (Domain != null) + { + ReturnValue = true; + } + else + { + if (AcceptWildcard) + { + DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; + if (DomainWild != null) + { + ReturnValue = true; + } + } + } + + return ReturnValue; + } + + /// + /// Get the Environment name for the current domain + /// + /// + public static string DomainEnvironmentName() + { + string ReturnValue = ""; + + //Get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[CurrentDomain]; + + ReturnValue = Domain.Environment; + + return ReturnValue; + } + + /// + /// Get the Environment name for the provided domain url + /// + /// Url to lookup + /// + public static string DomainEnvironmentName(string DomainUrl) + { + string ReturnValue = ""; + + //Get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; + + if (Domain != null) + { + ReturnValue = Domain.Environment; + } + else + { + //look for wildcard domain + DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; + if (DomainWild != null) + { + ReturnValue = DomainWild.Environment; + } + else + { + string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); + throw new MissingDomainConfigException(ErrorMsg); + } + } + + return ReturnValue; + } + + /// + /// Get the Site Name for the current domain + /// + /// + public static string DomainSiteName() + { + string ReturnValue = ""; + + //Get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[CurrentDomain]; + + ReturnValue = Domain.SiteName; + + return ReturnValue; + } + + /// + /// Get the Site Name for the provided domain url + /// + /// Url to lookup + /// + public static string DomainSiteName(string DomainUrl) + { + string ReturnValue = ""; + + //Get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; + + if (Domain != null) + { + ReturnValue = Domain.SiteName; + } + else + { + //look for wildcard domain + DomainElement DomainWild = ConfigSettings.Settings.Domains["*"]; + if (DomainWild != null) + { + ReturnValue = DomainWild.SiteName; + } + else + { + string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); + throw new MissingDomainConfigException(ErrorMsg); + } + } + + return ReturnValue; + } + + #endregion + + #region Environment Info + + /// + /// Check whether the current environment exists in the Environments list + /// + /// If there is a "default" domain specified, return true? (Choose false to explicitly search for this environment) + /// + public static bool EnvironmentIsConfigured(bool AcceptDefault = true) + { + return EnvironmentIsConfigured(DomainEnvironmentName(), AcceptDefault); + } + + /// + /// Check whether the current environment exists in the Environments list + /// + /// Name to lookup + /// If there is a "default" domain specified, return true? (Choose false to explicitly search for this environment) + /// + public static bool EnvironmentIsConfigured(string EnvironmentName, bool AcceptDefault = true) + { + bool ReturnValue = false; + + //Try to get the environment from config file + EnvironmentElement Env = ConfigSettings.Settings.Environments[EnvironmentName]; + + if (Env != null) + { + ReturnValue = true; + } + else + { + if (AcceptDefault) + { + EnvironmentElement EnvDefault = ConfigSettings.Settings.Environments["default"]; + if (EnvDefault != null) + { + ReturnValue = true; + } + } + } + + return ReturnValue; + } + + internal static EnvironmentElement GetDomainEnvironment(string DomainUrl) + { + string EnvName = ""; + try + { + //Get domain from config file + DomainElement Domain = ConfigSettings.Settings.Domains[DomainUrl]; + EnvName = Domain.Environment; + } + catch (Exception ) + { + try + { + DomainElement Domain = ConfigSettings.Settings.Domains["*"]; + EnvName = Domain.Environment; + } + catch (Exception Exception2) + { + string ErrorMsg = string.Format("Domain '{0}' is not configured, and there is no wildcard (*) domain configured", DomainUrl); + throw new MissingDomainConfigException(ErrorMsg, Exception2); + } + } + + if (EnvName != "") + { + EnvironmentElement ReturnValue = ConfigSettings.Settings.Environments[EnvName]; + return ReturnValue; + } + else + { + return null; + } + } + + internal static EnvironmentElement GetEnvironmentByName(string EnvironmentName) + { + EnvironmentElement ReturnValue; + + try + { + //Get environment from config file + ReturnValue = ConfigSettings.Settings.Environments[EnvironmentName]; + } + catch (Exception ) + { + try + { + //look for default environment + ReturnValue = ConfigSettings.Settings.Environments["default"]; + } + catch (Exception Exception2) + { + string ErrorMsg = string.Format("Environment '{0}' is not configured, and there is no 'default' environment configured", EnvironmentName); + throw new MissingEnvironmentConfigException(ErrorMsg, Exception2); + } + } + + return ReturnValue; + } + + #endregion + + #region Key/Value Configs + + /// + /// Get the value for a given key on the current domain + /// + /// Key name + /// + public static string GetValue(string ConfigKey) + { + string ReturnValue = ""; + ReturnValue = GetValue(ConfigKey, DomainEnvironmentName()); + + return ReturnValue; + } + + /// + /// Get the value when providing a key and environment name + /// + /// Key name + /// Environment name + /// + public static string GetValue(string ConfigKey, string EnvironmentName) + { + string ReturnValue = ""; + + //Get environment from config file + EnvironmentElement Env = GetEnvironmentByName(EnvironmentName); + + try + { + //find matching key + KeyValueElement KeyValue = Env.Configs[ConfigKey]; + + if (KeyValue != null) + { + ReturnValue = KeyValue.Value; + } + else + { + //Look for 'default' environment + EnvironmentElement EnvDefault = GetEnvironmentByName("default"); + + //find matching key + KeyValueElement KeyValueDefault = EnvDefault.Configs[ConfigKey]; + + ReturnValue = KeyValueDefault.Value; + } + } + catch (Exception Exception1) + { + string ErrorMsg = string.Format("Key '{0}' is not configured for Environment '{1}', and there is no default key configured", ConfigKey, EnvironmentName); + throw new MissingConfigKeyException(ErrorMsg, Exception1); + } + + return ReturnValue; + } + + + /// + /// Get all the KeyValue elements for the current Environment + /// + /// Include all ‘default’ KeyValue configs for keys not specifically defined for the environment. + /// + public static List AllEnvironmentConfigs(bool IncludeDefaults = true) + { + return AllEnvironmentConfigs(DomainEnvironmentName(), IncludeDefaults); + } + + /// + /// Get all the KeyValue elements for the current Environment + /// + /// Environment to get values from + /// Include all ‘default’ KeyValue configs for keys not specifically defined for the environment. + /// + public static List AllEnvironmentConfigs(string EnvironmentName, bool IncludeDefaults = true) + { + List ReturnList = new List(); + + EnvironmentElement Env = ConfigSettings.Settings.Environments[EnvironmentName]; + KeyValueElementCollection KvColl = Env.Configs; + + foreach (KeyValueElement KeyValue in Env.Configs) + { + ReturnList.Add(KeyValue); + } + + if (IncludeDefaults) + { + EnvironmentElement EnvDefault = ConfigSettings.Settings.Environments["default"]; + + foreach (KeyValueElement KeyValueDef in EnvDefault.Configs) + { + if (KvColl[KeyValueDef.Key]==null) + { + ReturnList.Add(KeyValueDef); + } + } + } + + return ReturnList; + } + + #endregion + + #region SMTP settings + /// + /// Get the SMTP Settings for the current domain + /// + /// Include all ‘default’ smtp configs for properties not specifically defined for the current domain. + /// + public static SmtpSettingsElement GetSmtpSettings(bool IncludeDefaults = true) + { + SmtpSettingsElement ReturnValue = null; + ReturnValue = GetSmtpSettings(DomainEnvironmentName(), IncludeDefaults); + + return ReturnValue; + } + + /// + /// Get the SMTP Settings for a given environment + /// + /// Environment to get smtp config from + /// Include all ‘default’ smtp configs for properties not specifically defined for the current domain. + /// + public static SmtpSettingsElement GetSmtpSettings(string EnvironmentName, bool IncludeDefaults = true) + { + SmtpSettingsElement ReturnValue = new SmtpSettingsElement(); + + EnvironmentElement Env = ConfigSettings.Settings.Environments[EnvironmentName]; + EnvironmentElement DefaultEnv = ConfigSettings.Settings.Environments["default"]; + + // fills the return value with the values from the requested environment + if (Env != null) + FillUndefinedSmtpSettings(ReturnValue, Env.SmtpSettings); + // fills the still undefined properties of the return value with the values from the default environment + if (IncludeDefaults && DefaultEnv != null) + FillUndefinedSmtpSettings(ReturnValue, DefaultEnv.SmtpSettings); + + return ReturnValue; + } + + /// + /// Performs a property per property fill of undefined properties of an SmtpSettingsElement based on the values of another one + /// + /// The SmtpSettingsElement to fill + /// The SmtpSettingsElement to get values from + private static void FillUndefinedSmtpSettings(SmtpSettingsElement Target, SmtpSettingsElement Source) + { + if (Target.IsPropertyUndefined(SmtpSettingsElement.DeliveryFormatPropertyName) && !Source.IsPropertyUndefined(SmtpSettingsElement.DeliveryFormatPropertyName)) + Target.DeliveryFormat= Source.DeliveryFormat; + + if (Target.IsPropertyUndefined(SmtpSettingsElement.DeliveryMethodPropertyName) && !Source.IsPropertyUndefined(SmtpSettingsElement.DeliveryMethodPropertyName)) + Target.DeliveryMethod= Source.DeliveryMethod; + + if (Target.IsPropertyUndefined(SmtpSettingsElement.FromPropertyName) && !Source.IsPropertyUndefined(SmtpSettingsElement.FromPropertyName)) + Target.From = Source.From; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.ClientDomainPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.ClientDomainPropertyName)) + Target.Network.ClientDomain = Source.Network.ClientDomain; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.DefaultCredentialsPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.DefaultCredentialsPropertyName)) + Target.Network.UseDefaultCredentials = Source.Network.UseDefaultCredentials; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.EnableSslPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.EnableSslPropertyName)) + Target.Network.EnableSsl = Source.Network.EnableSsl; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.HostPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.HostPropertyName)) + Target.Network.Host = Source.Network.Host; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.PasswordPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.PasswordPropertyName)) + Target.Network.Password = Source.Network.Password; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.PortPropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.PortPropertyName)) + Target.Network.Port = Source.Network.Port; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.TargetNamePropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.TargetNamePropertyName)) + Target.Network.TargetName = Source.Network.TargetName; + + if (Target.Network.IsPropertyUndefined(SmtpNetworkElement.UserNamePropertyName) && !Source.Network.IsPropertyUndefined(SmtpNetworkElement.UserNamePropertyName)) + Target.Network.UserName = Source.Network.UserName; + + if (Target.SpecifiedPickupDirectory.IsPropertyUndefined(SmtpSpecifiedPickupDirectoryElement.PickupDirectoryLocationPropertyName) && !Source.SpecifiedPickupDirectory.IsPropertyUndefined(SmtpSpecifiedPickupDirectoryElement.PickupDirectoryLocationPropertyName)) + Target.SpecifiedPickupDirectory.PickupDirectoryLocation = Source.SpecifiedPickupDirectory.PickupDirectoryLocation; + } + #endregion + } + + #region *** Custom Exceptions *** + + [Serializable] + internal class MissingDomainConfigException : Exception + { + // Use the default ApplicationException constructors + public MissingDomainConfigException() : base() + { + } + + public MissingDomainConfigException(string s) : base(s) + { + } + + public MissingDomainConfigException(string s, Exception ex) : base(s, ex) + { + } + } + + [Serializable] + internal class MissingEnvironmentConfigException : Exception + { + // Use the default ApplicationException constructors + public MissingEnvironmentConfigException() : base() + { + } + + public MissingEnvironmentConfigException(string s) : base(s) + { + } + + public MissingEnvironmentConfigException(string s, Exception ex) : base(s, ex) + { + } + } + + [Serializable] + internal class MissingConfigKeyException : Exception + { + // Use the default ApplicationException constructors + public MissingConfigKeyException() : base() + { + } + + public MissingConfigKeyException(string s) : base(s) + { + } + + public MissingConfigKeyException(string s, Exception ex) : base(s, ex) + { + } + } + + #endregion + +} + diff --git a/Source/TestSite/Default.aspx.cs b/Source/TestSite/Default.aspx.cs index 757965d..e4263d0 100644 --- a/Source/TestSite/Default.aspx.cs +++ b/Source/TestSite/Default.aspx.cs @@ -1,268 +1,327 @@ -using System; -using System.Configuration; -using System.Configuration.Internal; -using System.Reflection; -using HLF.ContextConfig; - -namespace TestSite -{ - public partial class Default : System.Web.UI.Page - { - protected void Page_Load(object sender, EventArgs e) - { - Response.Write("Hello!

"); - - Response.Write("

All Configuration Data

"); - Response.Write("

(Using the 'ConfigSettings' class)

"); - WriteData(); - Response.Write("
"); - - Response.Write("

Testing Functions

"); - Response.Write("

(Using the 'ContextConfig' class)

"); - LookupStuff(); - Response.Write("
"); - - Response.Write("

Testing Web.config Override

"); - Response.Write("

(Using the 'ContextConfigOverride' class)

"); - TestConfigOverride(); - Response.Write("
"); - } - - private void WriteData() - { - string Version = ConfigSettings.Settings.Version; - Response.Write(string.Format("Version: {0}
", Version)); - Response.Write("
"); - - int DomainsCount = ConfigSettings.Settings.Domains.Count; - Response.Write(string.Format("Total Domains: {0}
", DomainsCount)); - - int d = 0; - foreach (DomainElement Domain in ConfigSettings.Settings.Domains) - { - Response.Write(string.Format("Domain {0} : {1} = {2} ({3})
", d, Domain.Url, Domain.Environment, Domain.SiteName)); - d++; - } - Response.Write("
"); - - int EnvironmentsCount = ConfigSettings.Settings.Environments.Count; - Response.Write(string.Format("Total Environments: {0}
", EnvironmentsCount)); - - int e = 0; - foreach (EnvironmentElement Env in ConfigSettings.Settings.Environments) - { - Response.Write(string.Format("Environment {0} : {1}
", e, Env.Name)); - e++; - - int ConfigsCount = Env.Configs.Count; - Response.Write(string.Format("ConfigsCount: {0}
    ", ConfigsCount)); - - int c = 0; - foreach (KeyValueElement KeyValue in Env.Configs) - { - Response.Write(string.Format("
  • {0} = {1}
  • ", KeyValue.Key, KeyValue.Value)); - c++; - } - Response.Write("
"); - } - Response.Write("
"); - - } - - private void LookupStuff() - { - try - { - string B = ContextConfig.CurrentDomain; - Response.Write(string.Format("ContextConfig.CurrentDomain = {0}
", B)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string D = ContextConfig.DomainIsConfigured().ToString(); - Response.Write(string.Format("ContextConfig.DomainIsConfigured() = {0}
", D)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - - string F = ContextConfig.DomainIsConfigured("xyz.com").ToString(); - Response.Write(string.Format("ContextConfig.DomainIsConfigured(\"xyz.com\") = {0}
", F)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string G = ContextConfig.DomainIsConfigured("xyz.com", false).ToString(); - Response.Write(string.Format("ContextConfig.DomainIsConfigured(\"xyz.com\", false) = {0}
", G)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string E = ContextConfig.DomainEnvironmentName(); - Response.Write(string.Format("ContextConfig.DomainEnvironmentName() = {0}
", E)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string H = ContextConfig.DomainEnvironmentName("xyz.com"); - Response.Write(string.Format("ContextConfig.DomainEnvironmentName(\"xyz.com\") = {0}
", H)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - Response.Write("
"); - - try - { - string E = ContextConfig.DomainSiteName(); - Response.Write(string.Format("ContextConfig.DomainSiteName() = {0}
", E)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string H = ContextConfig.DomainSiteName("xyz.com"); - Response.Write(string.Format("ContextConfig.DomainSiteName(\"xyz.com\") = {0}
", H)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - Response.Write("
"); - - try - { - string I = ContextConfig.EnvironmentIsConfigured().ToString(); - Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured() = {0}
", I)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string I = ContextConfig.DomainEnvironmentName().ToString(); - Response.Write(string.Format("ContextConfig.DomainEnvironmentName() = {0}
", I)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string J = ContextConfig.EnvironmentIsConfigured("testing").ToString(); - Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured(\"testing\") = {0}
", J)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string K = ContextConfig.EnvironmentIsConfigured("testing", false).ToString(); - Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured(\"testing\", false) = {0}
", K)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - Response.Write("
"); - - try - { - string C = ContextConfig.GetValue("MyAppKey"); - Response.Write(string.Format("ContextConfig.GetValue(\"MyAppKey\") = {0}
", C)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string A = ContextConfig.GetValue("MyAppKey", "dev"); - Response.Write(string.Format("ContextConfig.GetValue(\"MyAppKey\", \"dev\") = {0}
", A)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string L = ContextConfig.GetValue("LiveOnlyAppKey", "live"); - Response.Write(string.Format("ContextConfig.GetValue(\"LiveOnlyAppKey\", \"live\") = {0}
", L)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string M = ContextConfig.GetValue("LiveOnlyAppKey"); - Response.Write(string.Format("ContextConfig.GetValue(\"LiveOnlyAppKey\") = {0}
", M)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - try - { - string N = ContextConfig.GetValue("KeyThatDoesntExist"); - Response.Write("Here we throw an error intentionally:"); - Response.Write(string.Format("ContextConfig.GetValue(\"KeyThatDoesntExist\") = {0}
", N)); - } - catch (Exception Ex) - { - Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); - } - - } - - private void TestConfigOverride() - { - Response.Write(string.Format("'OverrideConfigurationManager' value in config = {0}
", ConfigSettings.Settings.OverrideConfigurationManager)); - - // test it - string Test = ConfigurationManager.AppSettings["OverrideTest"]; - Response.Write(string.Format("OverrideTest = {0}
", Test)); - - // test missing value - string TestNull = ConfigurationManager.AppSettings["NoKey"]; - Response.Write(string.Format("Missing Key = {0}
", TestNull)); - - } - } +using System; +using System.Configuration; +using System.Configuration.Internal; +using System.Reflection; +using HLF.ContextConfig; + +namespace TestSite +{ + public partial class Default : System.Web.UI.Page + { + protected void Page_Load(object sender, EventArgs e) + { + Response.Write("Hello!

"); + + Response.Write("

All Configuration Data

"); + Response.Write("

(Using the 'ConfigSettings' class)

"); + WriteData(); + Response.Write("
"); + + Response.Write("

Testing Functions

"); + Response.Write("

(Using the 'ContextConfig' class)

"); + LookupStuff(); + Response.Write("
"); + + Response.Write("

Testing Web.config Override

"); + Response.Write("

(Using the 'ContextConfigOverride' class)

"); + TestConfigOverride(); + Response.Write("
"); + + } + + private void WriteData() + { + string Version = ConfigSettings.Settings.Version; + Response.Write(string.Format("Version: {0}
", Version)); + Response.Write("
"); + + int DomainsCount = ConfigSettings.Settings.Domains.Count; + Response.Write(string.Format("Total Domains: {0}
", DomainsCount)); + + int d = 0; + foreach (DomainElement Domain in ConfigSettings.Settings.Domains) + { + Response.Write(string.Format("Domain {0} : {1} = {2} ({3})
", d, Domain.Url, Domain.Environment, Domain.SiteName)); + d++; + } + Response.Write("
"); + + int EnvironmentsCount = ConfigSettings.Settings.Environments.Count; + Response.Write(string.Format("Total Environments: {0}
", EnvironmentsCount)); + + int e = 0; + foreach (EnvironmentElement Env in ConfigSettings.Settings.Environments) + { + Response.Write(string.Format("Environment {0} : {1}
", e, Env.Name)); + e++; + + int ConfigsCount = Env.Configs.Count; + Response.Write(string.Format("ConfigsCount: {0}
    ", ConfigsCount)); + + int c = 0; + foreach (KeyValueElement KeyValue in Env.Configs) + { + Response.Write(string.Format("
  • {0} = {1}
  • ", KeyValue.Key, KeyValue.Value)); + c++; + } + Response.Write("
"); + + Response.Write("SMTP Settings
    "); + var smtpSettings = Env.SmtpSettings; + Response.Write(string.Format("
  • From = {0}
  • ", smtpSettings.From)); + Response.Write(string.Format("
  • Delivery Method = {0}
  • ", smtpSettings.DeliveryMethod)); + Response.Write(string.Format("
  • Delivery Format = {0}
  • ", smtpSettings.DeliveryFormat)); + Response.Write(string.Format("
  • Use Default Credentials = {0}
  • ", smtpSettings.Network.UseDefaultCredentials)); + Response.Write(string.Format("
  • Enable SSL = {0}
  • ", smtpSettings.Network.EnableSsl)); + Response.Write(string.Format("
  • Host = {0}
  • ", smtpSettings.Network.Host)); + Response.Write(string.Format("
  • Port = {0}
  • ", smtpSettings.Network.Port)); + Response.Write(string.Format("
  • Client Domain = {0}
  • ", smtpSettings.Network.ClientDomain)); + Response.Write(string.Format("
  • User Name = {0}
  • ", smtpSettings.Network.UserName)); + Response.Write(string.Format("
  • Password = {0}
  • ", smtpSettings.Network.Password)); + Response.Write(string.Format("
  • Target Name = {0}
  • ", smtpSettings.Network.TargetName)); + Response.Write(string.Format("
  • Pickup Directory Location = {0}
  • ", smtpSettings.SpecifiedPickupDirectory.PickupDirectoryLocation)); + Response.Write("
"); + + } + Response.Write("
"); + } + + private void LookupStuff() + { + try + { + string B = ContextConfig.CurrentDomain; + Response.Write(string.Format("ContextConfig.CurrentDomain = {0}
", B)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string D = ContextConfig.DomainIsConfigured().ToString(); + Response.Write(string.Format("ContextConfig.DomainIsConfigured() = {0}
", D)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + + string F = ContextConfig.DomainIsConfigured("xyz.com").ToString(); + Response.Write(string.Format("ContextConfig.DomainIsConfigured(\"xyz.com\") = {0}
", F)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string G = ContextConfig.DomainIsConfigured("xyz.com", false).ToString(); + Response.Write(string.Format("ContextConfig.DomainIsConfigured(\"xyz.com\", false) = {0}
", G)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string E = ContextConfig.DomainEnvironmentName(); + Response.Write(string.Format("ContextConfig.DomainEnvironmentName() = {0}
", E)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string H = ContextConfig.DomainEnvironmentName("xyz.com"); + Response.Write(string.Format("ContextConfig.DomainEnvironmentName(\"xyz.com\") = {0}
", H)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + Response.Write("
"); + + try + { + string E = ContextConfig.DomainSiteName(); + Response.Write(string.Format("ContextConfig.DomainSiteName() = {0}
", E)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string H = ContextConfig.DomainSiteName("xyz.com"); + Response.Write(string.Format("ContextConfig.DomainSiteName(\"xyz.com\") = {0}
", H)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + Response.Write("
"); + + try + { + string I = ContextConfig.EnvironmentIsConfigured().ToString(); + Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured() = {0}
", I)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string I = ContextConfig.DomainEnvironmentName().ToString(); + Response.Write(string.Format("ContextConfig.DomainEnvironmentName() = {0}
", I)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string J = ContextConfig.EnvironmentIsConfigured("testing").ToString(); + Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured(\"testing\") = {0}
", J)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string K = ContextConfig.EnvironmentIsConfigured("testing", false).ToString(); + Response.Write(string.Format("ContextConfig.EnvironmentIsConfigured(\"testing\", false) = {0}
", K)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + Response.Write("
"); + + try + { + string C = ContextConfig.GetValue("MyAppKey"); + Response.Write(string.Format("ContextConfig.GetValue(\"MyAppKey\") = {0}
", C)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string A = ContextConfig.GetValue("MyAppKey", "dev"); + Response.Write(string.Format("ContextConfig.GetValue(\"MyAppKey\", \"dev\") = {0}
", A)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string L = ContextConfig.GetValue("LiveOnlyAppKey", "live"); + Response.Write(string.Format("ContextConfig.GetValue(\"LiveOnlyAppKey\", \"live\") = {0}
", L)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string M = ContextConfig.GetValue("LiveOnlyAppKey"); + Response.Write(string.Format("ContextConfig.GetValue(\"LiveOnlyAppKey\") = {0}
", M)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + try + { + string N = ContextConfig.GetValue("KeyThatDoesntExist"); + Response.Write("Here we throw an error intentionally:"); + Response.Write(string.Format("ContextConfig.GetValue(\"KeyThatDoesntExist\") = {0}
", N)); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + + Response.Write("
"); + + try + { + SmtpSettingsElement smtpSettings = ContextConfig.GetSmtpSettings(); + + Response.Write("SMTP Settings with default
    "); + Response.Write(string.Format("
  • From = {0}
  • ", smtpSettings.From)); + Response.Write(string.Format("
  • Delivery Method = {0}
  • ", smtpSettings.DeliveryMethod)); + Response.Write(string.Format("
  • Delivery Format = {0}
  • ", smtpSettings.DeliveryFormat)); + Response.Write(string.Format("
  • Use Default Credentials = {0}
  • ", smtpSettings.Network.UseDefaultCredentials)); + Response.Write(string.Format("
  • Enable SSL = {0}
  • ", smtpSettings.Network.EnableSsl)); + Response.Write(string.Format("
  • Host = {0}
  • ", smtpSettings.Network.Host)); + Response.Write(string.Format("
  • Port = {0}
  • ", smtpSettings.Network.Port)); + Response.Write(string.Format("
  • Client Domain = {0}
  • ", smtpSettings.Network.ClientDomain)); + Response.Write(string.Format("
  • User Name = {0}
  • ", smtpSettings.Network.UserName)); + Response.Write(string.Format("
  • Password = {0}
  • ", smtpSettings.Network.Password)); + Response.Write(string.Format("
  • Target Name = {0}
  • ", smtpSettings.Network.TargetName)); + Response.Write(string.Format("
  • Pickup Directory Location = {0}
  • ", smtpSettings.SpecifiedPickupDirectory.PickupDirectoryLocation)); + Response.Write("
"); + + smtpSettings = ContextConfig.GetSmtpSettings(false); + + Response.Write("SMTP Settings without default
    "); + Response.Write(string.Format("
  • From = {0}
  • ", smtpSettings.From)); + Response.Write(string.Format("
  • Delivery Method = {0}
  • ", smtpSettings.DeliveryMethod)); + Response.Write(string.Format("
  • Delivery Format = {0}
  • ", smtpSettings.DeliveryFormat)); + Response.Write(string.Format("
  • Use Default Credentials = {0}
  • ", smtpSettings.Network.UseDefaultCredentials)); + Response.Write(string.Format("
  • Enable SSL = {0}
  • ", smtpSettings.Network.EnableSsl)); + Response.Write(string.Format("
  • Host = {0}
  • ", smtpSettings.Network.Host)); + Response.Write(string.Format("
  • Port = {0}
  • ", smtpSettings.Network.Port)); + Response.Write(string.Format("
  • Client Domain = {0}
  • ", smtpSettings.Network.ClientDomain)); + Response.Write(string.Format("
  • User Name = {0}
  • ", smtpSettings.Network.UserName)); + Response.Write(string.Format("
  • Password = {0}
  • ", smtpSettings.Network.Password)); + Response.Write(string.Format("
  • Target Name = {0}
  • ", smtpSettings.Network.TargetName)); + Response.Write(string.Format("
  • Pickup Directory Location = {0}
  • ", smtpSettings.SpecifiedPickupDirectory.PickupDirectoryLocation)); + Response.Write("
"); + } + catch (Exception Ex) + { + Response.Write(string.Format("ERROR: {0} : {1}
", Ex.GetType().ToString(), Ex.Message)); + } + } + + private void TestConfigOverride() + { + Response.Write(string.Format("'OverrideConfigurationManager' value in config = {0}
", ConfigSettings.Settings.OverrideConfigurationManager)); + + // test it + string Test = ConfigurationManager.AppSettings["OverrideTest"]; + Response.Write(string.Format("OverrideTest = {0}
", Test)); + + // test missing value + string TestNull = ConfigurationManager.AppSettings["NoKey"]; + Response.Write(string.Format("Missing Key = {0}
", TestNull)); + + } + } } \ No newline at end of file diff --git a/Source/TestSite/Web.config b/Source/TestSite/Web.config index 0b3265e..dcdcbdd 100644 --- a/Source/TestSite/Web.config +++ b/Source/TestSite/Web.config @@ -16,12 +16,18 @@ - + + + + + + +