C# 配置文件中添加自定义section标签获取数据

前言:在添加获取配置文件自定义section 特性的数据时,遇到了问题并把它记录下来:

1. 类型“xxxxx.xxxx.sectionName”不从“System.Configuration.IConfigurationSectionHandler”继承。

这个问题应该算是一个常见的错误问题了。

解决办法:

1. 添加section标签,name为自定义标签名称,type为:类型的命名空间 + 类型,程序集名称

在配置节点下添加

<configSections>
    <section name="RedisConfig" type="Redis.Study.Config.RedisConfigInfo,Redis.Study"/>

2.RedisConfig为自定义标签,他的数据集为(ConfigurationSection)

  	<RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"
               MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false"></RedisConfig>
</configSections>

3.创建自定义标签的获取类,注意这里类的继承为数据集ConfigurationSection
这里需要对类的属性和标签属性使用:ConfigurationProperty(“标签属性”) 进行对应

   public  class RedisConfigInfo:ConfigurationSection
    {
     
        #region Property
        [ConfigurationProperty("WriteServerList")]
        public string WriteServerList {
     
            get {
      return  (string)this["WriteServerList"]; }
            set {
      this["WriteServerList"] = value; }
        }
        [ConfigurationProperty("ReadServerList")]
        public string ReadServerList
        {
     
            get {
      return (string)this["ReadServerList"]; }
            set {
      this["ReadServerList"] = value; }
        }
        [ConfigurationProperty("MaxReadPoolSize")]
        public int MaxReadPoolSize
        {
     
            get {
      return (int)this["MaxReadPoolSize"]; }
            set {
      this["MaxReadPoolSize"] = value; }
        }
        [ConfigurationProperty("MaxWritePoolSize")]
        public int MaxWritePoolSize
        {
     
            get {
      return (int)this["MaxWritePoolSize"]; }
            set {
      this["MaxWritePoolSize"] = value; }
        }
        [ConfigurationProperty("AutoStart")]
        public bool AutoStart
        {
     
            get {
      return (bool)this["AutoStart"]; }
            set {
      this["AutoStart"] = value; }
        }
        [ConfigurationProperty("RecordeLog")]
        public bool RecordeLog
        {
     
            get {
      return (bool)this["RecordeLog"]; }
            set {
      this["RecordeLog"] = value; }
        }
        [ConfigurationProperty("LocalCacheTime")]
        public int LocalCacheTime
        {
     
            get {
      return (int)this["LocalCacheTime"]; }
            set {
      this["LocalCacheTime"] = value; }
        }
        #endregion

      
    }

4.在此类中添加获取方法对配置文件Section进行获取
返回的 section 含有所有section对应标签值

  #region 获取配置方法
        public static RedisConfigInfo GetConfig() {
     
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");

            return section;
        }

        public static RedisConfigInfo GetConfig(string sectionName) {
     
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection(sectionName);
            if (section == null) {
     
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found");
            }

            return section;
        }

  #endregion

5.在过程中,主要遇到的问题是 继承问题与标签属性的对应。 当然还有很多更多细致的内容。后续在最一个补充吧

你可能感兴趣的:(.NET,常见问题分析,.net,redis,asp.net)