• 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>

    自定义节点名称

           /// <summary>
            /// sectionName
            /// </summary>
            /// <param name="sectionName">sectionName</param>
            /// <returns>RedisSettings</returns>
            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 配置类

        /// <summary>
        /// RedisSettings
        /// </summary>
        public sealed class RedisSettings : ConfigurationSection
        {
            /// <summary>
            /// GetConfig
            /// </summary>
            /// <returns>RedisSettings</returns>
            public static RedisSettings GetConfig()
            {
                RedisSettings section = GetConfig(RedisMappingConstants.RedisConfiguration);
                return section;
            }
    
            /// <summary>
            /// sectionName
            /// </summary>
            /// <param name="sectionName">sectionName</param>
            /// <returns>RedisSettings</returns>
            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;
            }
    
            /// <summary>
            /// DB
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.DbAttributeName, IsKey = true, IsRequired = true)]
            public int Db
            {
                get
                {
                    return (int)this[RedisMappingConstants.DbAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.DbAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 可写的Redis链接地址
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.WriteServerConStrAttributeName, IsRequired = true)]
            public string WriteServerConStr
            {
                get
                {
                    return (string)this[RedisMappingConstants.WriteServerConStrAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.WriteServerConStrAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 可读的Redis链接地址
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.ReadServerConStrAttributeName, IsRequired = true)]
            public string ReadServerConStr
            {
                get
                {
                    return (string)this[RedisMappingConstants.ReadServerConStrAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.ReadServerConStrAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 最大写链接数
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.MaxWritePoolSizeAttributeName, IsRequired = true)]
            public int MaxWritePoolSize
            {
                get
                {
                    return (int)this[RedisMappingConstants.MaxWritePoolSizeAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.MaxWritePoolSizeAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 最大写链接数
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.MaxReadPoolSizeAttributeName, IsRequired = true)]
            public int MaxReadPoolSize
            {
                get
                {
                    return (int)this[RedisMappingConstants.MaxReadPoolSizeAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.MaxReadPoolSizeAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 自动重启
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.AutoStartAttributeName, IsRequired = true)]
            public bool AutoStart
            {
                get
                {
                    return (bool)this[RedisMappingConstants.AutoStartAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.AutoStartAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 本地缓存到期时间,单位:秒
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.LocalCacheTimeAttributeName, IsRequired = true)]
            public int LocalCacheTime
            {
                get
                {
                    return (int)this[RedisMappingConstants.LocalCacheTimeAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.LocalCacheTimeAttributeName] = value;
                }
            }
    
            /// <summary>
            /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
            /// </summary>
            [ConfigurationProperty(RedisMappingConstants.RecordeLogAttributeName, IsRequired = true)]
            public bool RecordeLog
            {
                get
                {
                    return (bool)this[RedisMappingConstants.RecordeLogAttributeName];
                }
    
                set
                {
                    this[RedisMappingConstants.RecordeLogAttributeName] = value;
                }
            }
        }

    RedisMappingConstants 属性类

        /// <summary>
        /// 配置节点名称
        /// </summary>
        public class RedisMappingConstants
        {
            /// <summary>
            /// 配置文件映射节点
            /// </summary>
            public const string RedisConfiguration = "redisConfiguration";
    
            /// <summary>
            /// 数据库实例名称
            /// </summary>
            public const string DbAttributeName = "db";
    
            /// <summary>
            /// 可写的Redis链接地址
            /// </summary>
            public const string WriteServerConStrAttributeName = "writeServerConStr";
    
            /// <summary>
            /// 可读的Redis链接地址
            /// </summary>
            public const string ReadServerConStrAttributeName = "readServerConStr";
    
            /// <summary>
            /// 最大写链接数
            /// </summary>
            public const string MaxWritePoolSizeAttributeName = "maxWritePoolSize";
    
            /// <summary>
            /// 最大读链接数
            /// </summary>
            public const string MaxReadPoolSizeAttributeName = "maxReadPoolSize";
    
            /// <summary>
            /// 自动重启
            /// </summary>
            public const string AutoStartAttributeName = "autoStart";
    
            /// <summary>
            /// 本地缓存到期时间,单位:秒
            /// </summary>
            public const string LocalCacheTimeAttributeName = "localCacheTime";
    
            /// <summary>
            /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
            /// </summary>
            public const string RecordeLogAttributeName = "recordeLog";
        }
  • 相关阅读:
    英文、简繁体中文 IT 词汇对照表
    VB.NET 中的 As New 以及型別指定
    使用 ADO.NET 的 ExecuteScalar 方法返回单一值
    适时调整 SqlDataSource 控件的 DataSourceMode 属性
    ADO.NET 2.0 的并行控制与数据存取冲突侦测
    让 ADO.NET 2.0 的 SqlCommand 和 SqlDataAdapter 合作
    透过 Socket API 让 PDA 和远程 PC 联机
    探讨 .NET 语言的 using statement 与资源释放
    让 user control 中的 Button 也能启用验证
    dotNET 語言中可提升效能的邏輯運算子
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/7068806.html
Copyright © 2020-2023  润新知