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!好了,今天就写到这儿.

 

转载于:https://www.cnblogs.com/zhouxiaoyun/p/5804870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值