Ejemplo n.º 1
0
        private static ICustomConfigSection Build(string sectionName, ICustomConfigProvider provider = null)
        {
            if (provider == null)
            {
                provider = new DotNetConfigNameValueProvider(sectionName);
            }

            ICustomConfigSection section = new CustomConfigSection(sectionName);

            IEnumerable <CustomConfigItem> configList = null;

            try
            {
                configList = provider.Load();
            }
            catch (Exception ex)
            {
                //Kugar.Core.Log.LoggerManager.GetLogger().Error(string.Format("加载节点 {0} 的配置提供器 时出现错误{1}", sectionName,ex.Message));
            }

            if (configList != null)
            {
                foreach (var config in configList)
                {
                    section[config.Name] = config;
                }
            }

            return(section);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     使用Provider支持器添加一个配置节,如出现重复名称
        /// </summary>
        /// <param name="sectionName">指定配置节的名称</param>
        /// <param name="provider">配置节的支持器</param>
        public void Add(string sectionName, ICustomConfigProvider provider = null)
        {
            if (provider == null)
            {
                provider = new DotNetConfigNameValueProvider(sectionName);
            }

            if (string.IsNullOrWhiteSpace(sectionName))
            {
                throw new ArgumentNullException("sectionName");
            }

            if (_cache.ContainsKey(sectionName))
            {
                throw new ArgumentOutOfRangeException("sectionName", "不允许出现相同名称的配置节");
            }

            ICustomConfigSection section = new CustomConfigSection(sectionName);

            //if (provider is ILocalCustomConfigProvider)
            //{
            //    section = new LocalCustomSection(sectionName);
            //}
            //else
            //{
            //    section = new CustomConfigSection(sectionName);
            //}



            IEnumerable <CustomConfigItem> configList = null;

            try
            {
                configList = provider.Load();
            }
            catch (Exception ex)
            {
                //Kugar.Core.Log.LoggerManager.GetLogger().Error(string.Format("加载节点 {0} 的配置提供器 时出现错误{1}", sectionName,ex.Message));
            }

            if (configList != null)
            {
                foreach (var config in configList)
                {
                    section[config.Name] = config;
                }
            }

            _cache.Add(sectionName, section);

            _cacheProvider.Add(sectionName, provider);
        }