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";
}
App.Config自定义配置节点的更多相关文章
- C# App.config 自定义 配置节 出现的问题:配置系统未能初始化
C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...
- C# App.config 自定义 配置节
1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration> ...
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- VS2012 常用web.config配置解析之自定义配置节点
在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...
- 一个web.Config或app.Config自定义段configSections的示例
一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...
- ASP.NET系列:自定义配置节点的复用
appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
随机推荐
- springboot打包部署到tomcat
一. springboot打成war包: 1. 首先查看是否为war 2. File----->ProjectStruture,选择Artifacts,中部点击“+”号 3. 按图中标记进行选择 ...
- spring中的springSecurity安全框架的环境搭建
首先在web.xml文件中配置监听器和过滤器 <!--监听器 加载安全框架的核心配置文件到spring容器中--> <context-param> <param-name ...
- Docker学习笔记之了解 Docker 的核心组成
0x00 概述 在掌握 Docker 的一些背景知识后,我们还不得不花费一节的篇幅来简单介绍有关 Docker 核心的一些知识.当然,大家不要觉得有“核心”这类的词,我们就要在这一节中深入 Docke ...
- 我是这样做APP的:击中用户的痛点(转)
击中用户的痛点 点评,感觉取名叫做“用户痛点的取舍”更加合适.很多公司.项目的失败完全取决于决策人取舍的失败,一味地追求大而全.迎合上级领导,专断而没有和团队做客观的分析.本文虽然以一个应该来说并不复 ...
- 实现定时器定时 1 秒钟,LED 亮灭显示
实现定时器定时 1 秒钟,LED 亮灭显示 要求 每隔一秒钟,实现LED灯的显隐转换 实验代码 /*************************************************** ...
- Kali系列之hydra ssh密码爆破
环境 kali 192.168.137.131 靶机 192.168.137.133 语句 hydra -l root -P /home/chenglee/zidian/wordlist.TXT -t ...
- tf.argmax()以及axis解析
首先,明确一点,tf.argmax可以认为就是np.argmax.tensorflow使用numpy实现的这个API. 简单的说,tf.argmax就是返回最大的那个数值所在的下标. 这个 ...
- bzoj 2753 [SCOI 2012] 滑雪与时间胶囊 - Prim
题目传送门 传送点I 传送点II 题目大意 给定一个有$n$个点$m$条边的图,每个点有一个高度$h_{i}$,能从$u$经过一条边到达$v$,当且仅当存在一条边是$(u, v)$或$(v, u)$, ...
- topcoder srm 430 div1
problem1 link 其实就是找到一个数字$t$,使得$x$的二进制为1 的位上$t$也都为1.然后$t$删掉所有那些$x$为1的二进制位就是$k$. problem2 link 设所有合法的边 ...
- 从客户端(XXX)中检测到有潜在危险的Request.Form 值
aspx 页面出现 [HttpRequestValidationException (0x80004005):从客户端(TextBox1="<?xml version="1. ...