老实说,在以前没写个自定义配置节点之前,我都是写到一个很常用的节点里面,就是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#自定义配置文件节点的更多相关文章

  1. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  2. C#自定义配置文件节的实现

    1.配置文件:(注意configSections必须放在最上面否则会报错) <?xml version="1.0" encoding="utf-8" ?& ...

  3. C#自定义配置文件(一)

    C#自定义配置文件 .NET程序中,经常使用Config文件来配置应用程序中经常使用的值,比如数据库连接字符串.最近项目遇到一个需要配置好多节点在配置文件中的需求.为了使配置节点整洁易维护,在代码调用 ...

  4. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  5. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

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

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

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

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

  8. C#创建自定义配置节点

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  9. Spring Boot2.0自定义配置文件使用

    声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...

随机推荐

  1. PL/SQL 9 许可证

    code:j6stndb9tk72xfbhbqczcdqnjd8lyj466n number:882851 ps:xs374ca 我是PL/SQL版本号是: Version 9.0.3.1641 要注 ...

  2. SpringMVC传参

    @Controller @RequestMapping("/user") public UserController extends BaseController{ @InitBi ...

  3. LevelDB初体验

    近期工作须要找一个能使用磁盘存储数据,对写要求比較苛刻,须要每秒达100000TPS,读的时候须要能10000TPS左右,不能占用太多内存.单节点满足这个要求的常见有Redis.Memcached等, ...

  4. NSOJ Constructing Roads(图论)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  5. SSIS从理论到实战,再到应用

    原文:SSIS从理论到实战,再到应用 一,是什么(What?) 1.SSIS是Microsoft SQL Server Integration Services的简称,是生成高性能数据集成解决方案(包 ...

  6. Java数据结构与算法(21) - ch09红黑树(RB树)

    红-黑规则1. 每一个节点不是红色的就是黑色的2. 根总是黑色的3. 如果节点是红色的,则它的子节点必须是黑色的:如果节点是黑色的,其子节点不是必须为红色.4. 从根到叶节点或空子节点的每条路径,必须 ...

  7. zoj 3210 A Stack or A Queue? (数据结构水题)

     A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? ...

  8. Tair LDB基于Prefixkey中期范围内查找性能优化项目总结

    "Tair LDB基于Prefixkey该范围内查找性能优化"该项目是仅一个月.这个月主要是熟悉项目..以下从几个方面总结下个人在该项目上所做的工作及自己的个人所得所感. 项目工作 ...

  9. C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...

  10. linux_shell_类似sql的orderby 取最大值

    {} {} {} {} {} {} {} {} {} {} 需求场景 ,通过shell 筛选 每分钟的最大一条输出(最后一条) : .info | head