配置文件:

<?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. zabbix实现电话、短信、邮件报警

    该报警方式提前说明:(1)该方式可以实现zabbix免费电话报警以及微信.短信.邮件报警,但有数量限制.详见如下:如数量不能满足需要以及人员需要,可以考虑购买收费版.(2)毕竟是免费版,电话通知要省着 ...

  2. javaweb笔记—01(编程英语、常识、Tomcat配置问题)

    第一部分: 编程英语: legal:adj. 法律的:合法的:法定的 Userful :出版商  sponsor: n. 赞助者:主办者:保证人 | vt. 赞助:发起 essential:n. 本质 ...

  3. MyEclipse中项目运行时发生了Tomcat报错:[java.lang.OutOfMemoryError: PermGen space]

    Tomcat内存溢出,异常信息如下: 十一月 26, 2017 1:52:26 下午 org.apache.catalina.core.ContainerBase$ContainerBackgroun ...

  4. linux普通用户提权

    tar通配符注入. echo 'echo "chenglee ALL=(root) NOPASSWD: ALL" > /etc/sudoers' > demo.sh e ...

  5. linux判断日志文件大小进行清理

    脚本写了一个死循环,根据nohup产生的日志多大, 这里表示日志超过500M之后清理, 具体数字可自定义 睡眠数可自定义 #!/usr/bin/bash while true do s=`du -k ...

  6. 20165310 NetSec2019 Week6 Exp4 恶意代码分析

    20165310 NetSec2019 Week6 Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间 ...

  7. WIN10安装和使用MySql5.6中遇到的一些问题与解决

    WIN10安装和使用MySql5.6中遇到的一些问题与解决 提示一下,安装前需要安装python环境. MySql安装缺少组件MySQL for Excel 如图(转载别人的图,自己的安装时没有截图) ...

  8. NOI 2007 货币兑换Cash (bzoj 1492) - 斜率优化 - 动态规划 - CDQ分治

    Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...

  9. ListView与ArrayAdapter(二)

    ArrayAdapter: 数组适配器,用于简单的文字列表 activity_main.xml <RelativeLayout xmlns:android="http://schema ...

  10. Delphi XE5 for Android (九)

    Delphi XE5 下TEdit控件有个属性:KeyboardType,如下图:   该属性决定了当焦点进入TEdit时,系统弹出的输入窗体,按照其帮助文件说明,不同取值的输入窗体不同,如下图: 根 ...