/// <summary>
        /// Generate a lazy-load configurazion section using provided data
        /// </summary>
        /// <returns>Returns instance of configuration section</returns>
        private static TApplicationConfiguration GetConfiguration()
        {
            //Eseguo il recupero dell'attributo con il nome della configurazione
            ConfigurationSectionAttribute attribute = ReflectionUtils.
                                                      GetSingleAttribute <ConfigurationSectionAttribute>(typeof(TApplicationConfiguration), false);

            //Se l'attributo non è stato specificato, emetto eccezione
            if (attribute == null)
            {
                throw new InvalidProgramException(string.Format("Attribute of type '{0}' is required on type '{1}'.",
                                                                typeof(ConfigurationSectionAttribute).FullName, typeof(TApplicationConfiguration).FullName));
            }

            //Procedo alla lettura del file di configurazione applicativo
            TApplicationConfiguration local = ConfigurationManager.GetSection(attribute.TagName) as TApplicationConfiguration;

            //Se non ho recuperaro un oggetto valido, emetto un'eccezione
            if (local == null)
            {
                throw new ConfigurationErrorsException(string.Format("Unable to recognize a " +
                                                                     "valid configuration section for '{0}' in application configuration file.", attribute.TagName));
            }

            //Ritorno la configurazione
            return(local);
        }
Beispiel #2
0
        private T getSection <T>() where T : ConfigurationSection, new()
        {
            ConfigurationSectionAttribute csa = Attribute.GetCustomAttribute(typeof(T), typeof(ConfigurationSectionAttribute)) as ConfigurationSectionAttribute;
            string sectionName = (csa != null) ? csa.Name : typeof(T).Name;

            if (_config.GetSection(sectionName) == null)
            {
                _config.Sections.Add(sectionName, new T());
            }
            return(_config.GetSection(sectionName) as T);
        }
        /// <summary>
        /// Gets a section from this configuration source.
        /// </summary>
        /// <typeparam name="T">Type of the section that should be looked up.</typeparam>
        /// <returns>The section as it is configured in this source.</returns>
        public T GetSection <T>()
        {
            var sectionName = ConfigurationSectionAttribute.GetSectionName <T>();

            if (!_root.ContainsKey(sectionName))
            {
                return(default(T));
            }

            try
            {
                var serialized = JsonConvert.SerializeObject(_root[sectionName]);
                return(JsonConvert.DeserializeObject <T>(serialized));
            }
            catch (Exception exception)
            {
                throw new ConfigurationException($"Could not read configuration section {sectionName}. ", exception);
            }
        }
        private bool ValidateConfigurationSection(Object configSection, string sectionName, int indent)
        {
            string endStr = "\r\n";
            string preStr = "".PadRight(indent * 2);

            StringBuilder sb = new StringBuilder(preStr + "<" + sectionName);

            Type type = configSection.GetType();

            PropertyInfo[] propertiesInfo = type.GetProperties();

            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            ConfigurationAttributeAttribute attrib = customAttribs[j] as ConfigurationAttributeAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);

                                if (sectionName != null && sectionName == "cache-topology" && propertyValue is string)
                                {
                                    if (propertyValue is string)
                                    {
                                        if ((string)propertyValue == "local-cache")
                                        {
                                            _isLocal = true;
                                        }
                                    }
                                }

                                if (propertyValue == null && attrib.IsRequired)
                                {
                                    throw new Exception("Error: " + attrib.AttributeName + " attribute is missing in the specified configuration.");
                                }
                            }
                        }
                    }
                }
            }

            //get xml string for sub-sections if exists
            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            ConfigurationSectionAttribute attrib = customAttribs[j] as ConfigurationSectionAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                if (propertyValue != null)
                                {
                                    if (propertyValue.GetType().IsArray)
                                    {
                                        Array  array = propertyValue as Array;
                                        Object actualSectionObj;
                                        for (int k = 0; k < array.Length; k++)
                                        {
                                            actualSectionObj = array.GetValue(k);

                                            if (actualSectionObj != null)
                                            {
                                                ValidateConfigurationSection(actualSectionObj, attrib.SectionName, indent + 1);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ValidateConfigurationSection(propertyValue, attrib.SectionName, indent + 1);
                                    }
                                }
                                else if (propertyValue == null && attrib.IsRequired)
                                {
                                    if (attrib.SectionName is string)
                                    {
                                        if ((string)attrib.SectionName == "data-replication" && !_isLocal)
                                        {
                                            throw new Exception("Error: " + attrib.SectionName + " section is missing in the specified configuration.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
 public void ConfigurationSectionAttribute_GetSectionNameReturnsSectionNameOfType()
 {
     Assert.Equal("section", ConfigurationSectionAttribute
                  .GetSectionName <TypeWithConfigurationSectionAttribute>());
 }
 public void ConfigurationSectionAttribute_GetSectionNameThrowsInvalidOperationExceptionForTypeWithoutAttribute()
 {
     Assert.Throws <InvalidOperationException>(() =>
                                               ConfigurationSectionAttribute.GetSectionName <TypeWithoutConfigurationSectionAttribute>());
 }
Beispiel #7
0
        private bool ValidateConfigurationSection(Object configSection, string sectionName, int indent)
        {
            //string strPropertyValue = "";
            string endStr = "\r\n";
            string preStr = "".PadRight(indent * 2);

            StringBuilder sb = new StringBuilder(preStr + "<" + sectionName);

            Type type = configSection.GetType();

            PropertyInfo[] propertiesInfo = type.GetProperties();

            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            ConfigurationAttributeAttribute attrib = customAttribs[j] as ConfigurationAttributeAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                //string appendedText = attrib.AppendedText != null ? attrib.AppendedText : "";

                                if (sectionName != null && sectionName == "cache-topology" && propertyValue is string)
                                {
                                    if (propertyValue is string)
                                    {
                                        if ((string)propertyValue == "local-cache")
                                        {
                                            _isLocal = true;
                                        }
                                    }
                                }

                                if (propertyValue == null && attrib.IsRequired)
                                {
                                    throw new Exception("Error: " + attrib.AttributeName + " attribute is missing in the specified configuration.");
                                }
                                //else if (propertyValue != null)
                                //{
                                //    //strPropertyValue = propertyValue.ToString();
                                //    //if (sectionName == "attribute" && strPropertyValue.Contains("<"))
                                //    //{
                                //    //    strPropertyValue = strPropertyValue.Replace("<", "&lt;");
                                //    //    strPropertyValue = strPropertyValue.Replace(">", "&gt;");
                                //    //}
                                //    string value = propertyValue.ToString();

                                //    if (property.DeclaringType == typeof(Boolean))
                                //    {
                                //        value = value.ToLower();
                                //    }

                                //    sb.Append(" " + attrib.AttributeName + "=\"" + value + appendedText + "\"");
                                //}
                                //else
                                //{
                                //    sb.Append(" " + attrib.AttributeName + "=\"\"");
                                //}
                            }
                        }
                    }
                }
            }

            //bool subsectionsFound = false;
            //bool firstSubSection = true;
            //StringBuilder comments = null;

            //get xml string for sub-sections if exists
            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            //ConfigurationCommentAttribute commentAttrib = customAttribs[j] as ConfigurationCommentAttribute;
                            //if (commentAttrib != null)
                            //{
                            //    Object propertyValue = property.GetValue(configSection, null);
                            //    if (propertyValue != null)
                            //    {
                            //        string propStr = propertyValue as string;
                            //        if (!string.IsNullOrEmpty(propStr))
                            //        {
                            //            if (comments == null)
                            //            {
                            //                comments = new StringBuilder();
                            //            }
                            //            comments.AppendFormat("{0}<!--{1}-->{2}", preStr, propStr, endStr);
                            //        }
                            //    }
                            //}

                            ConfigurationSectionAttribute attrib = customAttribs[j] as ConfigurationSectionAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                if (propertyValue != null)
                                {
                                    //subsectionsFound = true;
                                    //if (firstSubSection)
                                    //{
                                    //    sb.Append(">" + endStr);
                                    //    firstSubSection = false;
                                    //}
                                    if (propertyValue.GetType().IsArray)
                                    {
                                        Array  array = propertyValue as Array;
                                        Object actualSectionObj;
                                        for (int k = 0; k < array.Length; k++)
                                        {
                                            actualSectionObj = array.GetValue(k);

                                            if (actualSectionObj != null)
                                            {
                                                ValidateConfigurationSection(actualSectionObj, attrib.SectionName, indent + 1);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ValidateConfigurationSection(propertyValue, attrib.SectionName, indent + 1);
                                    }
                                }
                                else if (propertyValue == null && attrib.IsRequired)
                                {
                                    if (attrib.SectionName is string)
                                    {
                                        if ((string)attrib.SectionName == "data-replication" && !_isLocal)
                                        {
                                            throw new Exception("Error: " + attrib.SectionName + " section is missing in the specified configuration.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //if (subsectionsFound)
            //    sb.Append(preStr + "</" + sectionName + ">" + endStr);
            //else
            //    sb.Append("/>" + endStr);

            //string xml = string.Empty;
            //if (comments != null)
            //{
            //    xml = comments.ToString() + sb.ToString();
            //}
            //else
            //{
            //    xml = sb.ToString();
            //}

            return(true);
        }