Пример #1
0
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<br />1. Calling GetValue():<br /><br />");

            try
            {
                string sAdminEmail = ConfigStore.GetValue("MyApplication", "AdminEmail");
                writer.Write(string.Format("Retrieved '{0}' for AdminEmail <br />", sAdminEmail));
            }
            catch (InvalidConfigurationException e)
            {
                writer.Write(string.Format("Exception occurred : {0}", e));
            }

            writer.Write("<br /><br />2. Now calling GetMultipleValues():<br /><br />");

            List <ConfigIdentifier> configIds      = new List <ConfigIdentifier>();
            ConfigIdentifier        adminEmail     = new ConfigIdentifier("MyApplication", "AdminEmail");
            ConfigIdentifier        workflowEmails = new ConfigIdentifier("MyApplication", "SendWorkflowEmails");

            configIds.Add(adminEmail);
            configIds.Add(workflowEmails);
            try
            {
                Dictionary <ConfigIdentifier, string> configItems = ConfigStore.GetMultipleValues(configIds);

                string sAdminEmails    = ConfigStoreHelper.ReadDictionaryValue(configItems, adminEmail, string.Empty);
                string sWorkflowEmails = ConfigStoreHelper.ReadDictionaryValue(configItems, workflowEmails, string.Empty);

                writer.Write(string.Format("Retrieved '{0}' for AdminEmail <br />", sAdminEmails));
                writer.Write(string.Format("Retrieved '{0}' for SendWorkflowEmails <br />", sWorkflowEmails));
            }
            catch (InvalidConfigurationException e)
            {
                writer.Write(string.Format("Exception occurred: {0}", e));
            }
        }
        /// <summary>
        /// Reads a value from a dictionary of config values retrieved by ConfigStore.GetValues(). This method ensures a meaningful
        /// exception is thrown is a config value is not present so the missing value can be easily identified.
        /// </summary>
        /// <param name="ConfigItems">Dictionary of config values.</param>
        /// <param name="ConfigID">ConfigIdentifier for the value to retrieve from dictionary.</param>
        /// <param name="MissingValueMessage">Optional - the message to include in exception thrown when no config value is retrieved,
        /// e.g. 'This config key should specify the ....'.</param>
        /// <returns>String config value.</returns>
        public static string ReadDictionaryValue(Dictionary <ConfigIdentifier, string> ConfigItems, ConfigIdentifier ConfigID,
                                                 string MissingValueMessage)
        {
            string sValue = null;

            try
            {
                sValue = ConfigItems[ConfigID];
            }
            catch (KeyNotFoundException)
            {
                // throw more meaningful exception and trace..

                MissingValueMessage = MissingValueMessage ?? string.Empty;
                string sError = string.Format("No value found in Config Store for key " +
                                              "{0}.{1}. {2}",
                                              ConfigID.Category, ConfigID.Key, MissingValueMessage);
                trace.WriteLineIf(traceSwitch.TraceError, TraceLevel.Error, "SafelyReadDictionaryValue(): {0}.", sError);
                throw new ConfigurationErrorsException(sError);
            }

            return(sValue);
        }