配置文件:

<?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";
}

App.Config自定义配置节点的更多相关文章

  1. C# App.config 自定义 配置节 出现的问题:配置系统未能初始化

    C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...

  2. C# App.config 自定义 配置节

    1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...

  3. C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法

    App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...

  4. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  5. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  6. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  7. ASP.NET系列:自定义配置节点的复用

    appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...

  8. 一个web.Config或app.Config自定义段configSections的示例--转

    直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...

  9. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

随机推荐

  1. Web前端开发(基础学习+坑)

    0.基本说明 0.内容为课堂所学基本知识,加自己踩过的坑 1.web基本框架:html+css+JavaScript,html为网页骨架,css为网页美化,JavaScript负责页面动态交互,脚本等 ...

  2. windows无法远程连接linux

    网络模式 修改对应的NAT模式,子网地址的前三位要与window,internet协议版本里的IP地址的前三位一致.

  3. 登陆 全站 user

    TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...

  4. JavaScript实现表单验证_02

    注册3次错误,最终的结果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  5. mysql/oracle jdbc大数据量插入优化

    10.10.6  大数据量插入优化 在很多涉及支付和金融相关的系统中,夜间会进行批处理,在批处理的一开始或最后一般需要将数据回库,因为应用和数据库通常部署在不同的服务器,而且应用所在的服务器一般也不会 ...

  6. truncate table很慢之enq: RO - fast object reuse和local write wait等待分析

    使用ASSM表空间(默认模式)的时候,在dss系统中确实会出现truncate很慢的现象,但是他不会100%重现,得看概率.通过sql trace(对任何v$sysstat看起来资源消耗很低的情况,都 ...

  7. gcc对c++标准的支持

    GCC 4.8.1完全支持c++11核心部分,对应的glibc为2.17 gcc 4.9支持c++11正则表达式,卧槽...4.8.5会报terminate called after throwing ...

  8. ajax返回数据

    在使用远程js验证检测账户是否存在时,直在发请求后返回值无效,怎样把值返回回来呢重点注意两点 第一点:type不能省略,不能是异步,async: false 第二点:不能在直接请求成功后返回 var ...

  9. python 之 函数的参数

    函数的参数好几种类型:包括位置参数.默认参数.可变参数.关键字参数.命名关键字参数. 廖大神python学习笔记,大神网站:百度搜索“廖雪峰的官网” 1.位置参数:调用函数时根据函数定义的参数位置来传 ...

  10. 深入浅出MyBatis-快速入门

    http://blog.csdn.net/hupanfeng/article/details/9068003/