配置文件:

<?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. java加载配置文件信息

    #基金数据存放根目录fund_save_root_path=E:/fundCrawling #龙虎榜数据存放根目录long_hu_root_path=E:/longHuCrawling #巨潮数据存放 ...

  2. linux下VLAN设置

    1. 安装vlan(vconfig)和加载8021q模块 [root@test0001~]#yum install vconfig [root@test0001~]#modprobe 8021q [r ...

  3. close yield

    close的方法主要是关闭子生成器,需要注意的有4点: 1.如果生成器close后,还继续next,会报错StopIteration   [图片]     2.如果我捕获了异常,将GeneratorE ...

  4. MD5算法详解

    MD5是什么 message-digest algorithm 5(信息-摘要算法).经常说的“MD5加密”,就是它→信息-摘要算法.在下载一下东西时,经常在一些压缩包属性里,看到md5值.而且这个下 ...

  5. centos6下jbd2进程占用大量IO处理

    刚在尝试重现一个bug时,好像在killed mysql一段时间之后,io一直很高,如下: 12:40:01 PM CPU %user %nice %system %iowait %steal %id ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

    题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...

  7. Restful framework【第十一篇】url路由控制

    基本使用 -url控制 -传统的url配置 url(r'^books/$', views.BookView.as_view()), url(r'^books/(?P<pk>\d+)$', ...

  8. POJ 2594 Treasure Exploration(最小可相交路径覆盖)题解

    题意:有n个点,m条单向边,每个机器人能沿着单向边走,能重复经过一个点,问最少几个机器人走遍n个点 思路:原来以前学的都是不能相交的算法....可相交的做法是跑Floyd把能到达的都加上边,然后跑最小 ...

  9. 分布式知识点总结(来自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/%E5%88%86%E5%B8%83%E5%BC%8F.md 注:如Paxos等的 ...

  10. facebook api之Access and Authentication

    Access and Authentication There are three access levels to the Marketing APIs. You can upgrade acces ...