C#自定义配置文件节点
老实说,在以前没写个自定义配置节点之前,我都是写到一个很常用的节点里面,就是appSettings里add,然后再对各个节点的value值进行字符串分割操作,根据各种分割字符嵌套循环处理,后来看到一些常用的框架都会加个configSections,然后百度后发现可以自己写配置文件节点,然后就在项目中定义自己的节点
其实,上面说的都是废话,现在项目中要用WCF服务,用传统的WCF配置工具或者手写的会有一堆配置,所以我稍作了简化,看下面的
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="test" type="TestService.Common.Config.TestServiceSection, TestService.Common" />
</configSections>
<test>
<add name="TestService" service="TestService.ApplicationService.TestService, TestService.ApplicationService"
contract="TestService.Contract.ITestService, TestService.Contract" uri="net.tcp://localhost:10001/TestService" />
<add name="FileService" service="TestService.ApplicationService.FileService, TestService.ApplicationService"
contract="TestService.Contract.IFileService, TestService.Contract" uri="net.tcp://localhost:10001/FileService" />
<add name="MessageService" service="TestService.ApplicationService.MessageService, TestService.ApplicationService"
contract="TestService.Contract.IMessageService, TestService.Contract" uri="net.tcp://localhost:10001/MessageService" />
</test>
</configuration>
上面的test就是我定义的节点,子节点是各个WCF的配置,看下TestServiceSection里面是个什么鬼
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text; namespace TestService.Common.Config
{
public sealed class TestServiceSection : ConfigurationSection
{
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(TheKeyValueCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public TheKeyValueCollection KeyValues
{
get
{
return (TheKeyValueCollection)base[s_property];
}
} public static TestServiceSection GetConfig()
{
TestServiceSection configSection= (TestServiceSection)ConfigurationManager.GetSection("test");
if (configSection == null)
throw new ConfigurationErrorsException("Section test is not found.");
return configSection;
} public static TestServiceSection GetConfig(string configPath)
{
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configPath };
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
TestServiceSection configSection = (TestServiceSection)config.GetSection("test");
if (configSection == null)
throw new ConfigurationErrorsException("Section test is not found.");
return configSection;
}
} [ConfigurationCollection(typeof(TheKeyValue))]
public class TheKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
new TheKeyValue this[string name]
{
get
{
return (TheKeyValue)base.BaseGet(name);
}
} // 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement()
{
return new TheKeyValue();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((TheKeyValue)element).Name;
}
} public class TheKeyValue : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return this["name"].ToString(); }
set { this["name"] = value; }
} [ConfigurationProperty("uri", IsRequired = true)]
public string Uri
{
get { return this["uri"].ToString(); }
set { this["uri"] = value; }
} [ConfigurationProperty("service", IsRequired = true)]
public string Service
{
get { return this["service"].ToString(); }
set { this["service"] = value; }
} [ConfigurationProperty("contract", IsRequired = true)]
public string Contract
{
get { return this["contract"].ToString(); }
set { this["contract"] = value; }
}
}
}
有了上面的一块代码,编译是冒得问题的,那么问题来了,怎么取自定义配置节点里的东西呢?
look
TestServiceSection services = TestServiceSection.GetConfig();
foreach (TheKeyValue item in services.KeyValues)
{
var i = item.ElementInformation; }
循环里面的就是配置节点里的信息,可以卡个断点看看,是不是so easy!好了,今天就写到这儿.
C#自定义配置文件节点的更多相关文章
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- C#自定义配置文件节的实现
1.配置文件:(注意configSections必须放在最上面否则会报错) <?xml version="1.0" encoding="utf-8" ?& ...
- C#自定义配置文件(一)
C#自定义配置文件 .NET程序中,经常使用Config文件来配置应用程序中经常使用的值,比如数据库连接字符串.最近项目遇到一个需要配置好多节点在配置文件中的需求.为了使配置节点整洁易维护,在代码调用 ...
- .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- VS2012 常用web.config配置解析之自定义配置节点
在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...
- C#创建自定义配置节点
转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...
- Spring Boot2.0自定义配置文件使用
声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...
随机推荐
- 大约SQL/NoSQL数据库搜索/思考查询
转载请注明出处:jiq•钦's technical Blog Hbase特征: 近期在学习Hbase.Hbase基于行健是建立了索引的,查询速度会很快,全然实时. 可是Hbase要基于行健之外的字段进 ...
- 开源项目:底部动作条(BottomSheet)
底部动作条(BottomSheet)是一个从屏幕底部边缘向上滑出的一个面板,给用户呈现一组功能选项.底部动作条封装了一组简单.清晰.无需额外说明的操作.底部动作条(如下图)可以是列表样式的,也可以是宫 ...
- SSIS从理论到实战,再到应用(6)----SSIS的自带日志功能
原文:SSIS从理论到实战,再到应用(6)----SSIS的自带日志功能 上期回顾: SSIS从理论到实战,再到应用(5)----流程控制之Foreach循环 博主最近新负责了一个ssis大项目的架构 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(8)-DbSession线程内唯一
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(8)-DbSession线程内唯一 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 ...
- 框架搭建资源 (二) 添加M(模型)
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- JavaEE Tutorials (2) - 使用教程示例
2.1 必要软件27 2.1.1 Java EE 7软件开发包28 2.1.2 Java平台标准版28 2.1.3 Java EE 7教程组件28 2.1.4 NetBeans IDE29 2.1.5 ...
- android USB OTG功能实现
一个.检查HW概要,确认是否支持OTG特征(vbus无论是动力,IDDIG pin连接是否正确) 两.如果HW确认支持OTG特征,然后根据以下方法被打开USB OTG功能及实现山: 如何打开USB O ...
- JavaScript 奇技淫巧
JavaScript 奇技淫巧 这里记录一下以前学习各种书籍和文章里边出现的JS的小技巧,分享给大家,也供自己查阅,同时感谢那些发现创造和分享这些技巧的前辈和大牛们. 1.遍历一个obj的属性到数组 ...
- linux下一个Oracle11g RAC建立(八)
linux下一个Oracle11g RAC建立(八) 七.安装oracle软件 直接在图形界面里安装oracle.在node1操作 在虚拟机界面中,直接切换到oracle用户下: [grid@no ...
- 设计模式---订阅发布模式(Subscribe/Publish)
设计模式---订阅发布模式(Subscribe/Publish) 订阅发布模式定义了一种一对多的依赖关系,让多个订阅者对象同时监听某一个主题对象.这个主题对象在自身状态变化时,会通知所有订阅者对象,使 ...