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!好了,今天就寫到這兒.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM