App.Config自定义配置节点

配置文件:

xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="redisConfiguration" type="Redis.Configuration.RedisSettings,Redis,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
  configSections>
  <redisConfiguration db="0" writeServerConStr="192.168.10.9:6379" readServerConStr="192.168.10.9:6379" maxWritePoolSize="100" maxReadPoolSize="100" autoStart="true" localCacheTime="31536000" recordeLog="false" />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  startup>
configuration>

自定义节点名称

       /// 
        /// sectionName
        /// 
        /// sectionName
        /// RedisSettings
        public static RedisSettings GetConfig(string sectionName)
        {
            RedisSettings section = (RedisSettings)ConfigurationManager.GetSection("redisConfiguration"/*自定义节点名称*/);
            if (section == null)
            {
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            }
            return section;
        }

RedisSettings 配置类

    /// 
    /// RedisSettings
    /// 
    public sealed class RedisSettings : ConfigurationSection
    {
        /// 
        /// GetConfig
        /// 
        /// RedisSettings
        public static RedisSettings GetConfig()
        {
            RedisSettings section = GetConfig(RedisMappingConstants.RedisConfiguration);
            return section;
        }

        /// 
        /// sectionName
        /// 
        /// sectionName
        /// RedisSettings
        public static RedisSettings GetConfig(string sectionName)
        {
            RedisSettings section = (RedisSettings)ConfigurationManager.GetSection(sectionName);
            if (section == null)
            {
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            }
            return section;
        }

        /// 
        /// DB
        /// 
        [ConfigurationProperty(RedisMappingConstants.DbAttributeName, IsKey = true, IsRequired = true)]
        public int Db
        {
            get
            {
                return (int)this[RedisMappingConstants.DbAttributeName];
            }

            set
            {
                this[RedisMappingConstants.DbAttributeName] = value;
            }
        }

        /// 
        /// 可写的Redis链接地址
        /// 
        [ConfigurationProperty(RedisMappingConstants.WriteServerConStrAttributeName, IsRequired = true)]
        public string WriteServerConStr
        {
            get
            {
                return (string)this[RedisMappingConstants.WriteServerConStrAttributeName];
            }

            set
            {
                this[RedisMappingConstants.WriteServerConStrAttributeName] = value;
            }
        }

        /// 
        /// 可读的Redis链接地址
        /// 
        [ConfigurationProperty(RedisMappingConstants.ReadServerConStrAttributeName, IsRequired = true)]
        public string ReadServerConStr
        {
            get
            {
                return (string)this[RedisMappingConstants.ReadServerConStrAttributeName];
            }

            set
            {
                this[RedisMappingConstants.ReadServerConStrAttributeName] = value;
            }
        }

        /// 
        /// 最大写链接数
        /// 
        [ConfigurationProperty(RedisMappingConstants.MaxWritePoolSizeAttributeName, IsRequired = true)]
        public int MaxWritePoolSize
        {
            get
            {
                return (int)this[RedisMappingConstants.MaxWritePoolSizeAttributeName];
            }

            set
            {
                this[RedisMappingConstants.MaxWritePoolSizeAttributeName] = value;
            }
        }

        /// 
        /// 最大写链接数
        /// 
        [ConfigurationProperty(RedisMappingConstants.MaxReadPoolSizeAttributeName, IsRequired = true)]
        public int MaxReadPoolSize
        {
            get
            {
                return (int)this[RedisMappingConstants.MaxReadPoolSizeAttributeName];
            }

            set
            {
                this[RedisMappingConstants.MaxReadPoolSizeAttributeName] = value;
            }
        }

        /// 
        /// 自动重启
        /// 
        [ConfigurationProperty(RedisMappingConstants.AutoStartAttributeName, IsRequired = true)]
        public bool AutoStart
        {
            get
            {
                return (bool)this[RedisMappingConstants.AutoStartAttributeName];
            }

            set
            {
                this[RedisMappingConstants.AutoStartAttributeName] = value;
            }
        }

        /// 
        /// 本地缓存到期时间,单位:秒
        /// 
        [ConfigurationProperty(RedisMappingConstants.LocalCacheTimeAttributeName, IsRequired = true)]
        public int LocalCacheTime
        {
            get
            {
                return (int)this[RedisMappingConstants.LocalCacheTimeAttributeName];
            }

            set
            {
                this[RedisMappingConstants.LocalCacheTimeAttributeName] = value;
            }
        }

        /// 
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
        /// 
        [ConfigurationProperty(RedisMappingConstants.RecordeLogAttributeName, IsRequired = true)]
        public bool RecordeLog
        {
            get
            {
                return (bool)this[RedisMappingConstants.RecordeLogAttributeName];
            }

            set
            {
                this[RedisMappingConstants.RecordeLogAttributeName] = value;
            }
        }
    }

RedisMappingConstants 属性类

    /// 
    /// 配置节点名称
    /// 
    public class RedisMappingConstants
    {
        /// 
        /// 配置文件映射节点
        /// 
        public const string RedisConfiguration = "redisConfiguration";

        /// 
        /// 数据库实例名称
        /// 
        public const string DbAttributeName = "db";

        /// 
        /// 可写的Redis链接地址
        /// 
        public const string WriteServerConStrAttributeName = "writeServerConStr";

        /// 
        /// 可读的Redis链接地址
        /// 
        public const string ReadServerConStrAttributeName = "readServerConStr";

        /// 
        /// 最大写链接数
        /// 
        public const string MaxWritePoolSizeAttributeName = "maxWritePoolSize";

        /// 
        /// 最大读链接数
        /// 
        public const string MaxReadPoolSizeAttributeName = "maxReadPoolSize";

        /// 
        /// 自动重启
        /// 
        public const string AutoStartAttributeName = "autoStart";

        /// 
        /// 本地缓存到期时间,单位:秒
        /// 
        public const string LocalCacheTimeAttributeName = "localCacheTime";

        /// 
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
        /// 
        public const string RecordeLogAttributeName = "recordeLog";
    }

 

转载于:https://www.cnblogs.com/liuxiaoji/p/7068806.html

你可能感兴趣的:(App.Config自定义配置节点)