【问题标题】:Unrecognised Element "Processor" - Custom Config Section无法识别的元素“处理器” - 自定义配置部分
【发布时间】:2015-04-29 22:59:54
【问题描述】:

尝试在此处的另一篇文章之后添加自定义配置部分,但出现错误:

未处理的类型异常 'System.Configuration.ConfigurationErrorsException' 发生在 System.Configuration.dll

附加信息:无法识别的元素“处理器”。

在这一行:

return (ProcessorsConfig)ConfigurationManager.GetSection("Processors") ?? new ProcessorsConfig();

这是我的代码:

public class ProcessorsConfig : ConfigurationSection
{
    public static ProcessorsConfig GetConfig()
    {
        return (ProcessorsConfig)ConfigurationManager.GetSection("Processors") ?? new ProcessorsConfig();
    }

    [ConfigurationProperty("Processors")]
    [ConfigurationCollection(typeof(Processors), AddItemName = "Processor")]
    public Processors Processors
    {
        get
        {
            object o = this["Processors"];
            return o as Processors;
        }
    }
}



[ConfigurationCollection(typeof(Processor), AddItemName="Processor", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class Processors : ConfigurationElementCollection
{
    public Processor this[int index]
    {
        get
        {
            return base.BaseGet(index) as Processor;
        }
        set
        {
            if (base.BaseGet(index) != null)
                base.BaseRemoveAt(index);

            this.BaseAdd(index, value);
        }
    }

    public new Processor this[string responseString]
    {
        get { return (Processor)BaseGet(responseString); }
        set
        {
            if (BaseGet(responseString)!=null)
                BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));

            BaseAdd(value);
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new Processor();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Processor)element).Name;
    }
}

public class Processor : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired=true)]
    public string Name
    {
        get
        {
            return this["name"] as string;
        }           
    }

    [ConfigurationProperty("searchPattern", IsRequired = true)]
    public string SearchPattern
    {
        get
        {
            return this["searchPattern"] as string;
        }
    }

    [ConfigurationProperty("collectionFolder", IsRequired = true)]
    public string CollectionFolder
    {
        get
        {
            return this["collectionFolder"] as string;
        }
    }

    [ConfigurationProperty("failureFolder", IsRequired = true)]
    public string FailureFolder
    {
        get
        {
            return this["failureFolder"] as string;
        }
    }

    [ConfigurationProperty("xmlOutputFolder", IsRequired = true)]
    public string XMLOutputFolder
    {
        get
        {
            return this["xmlOutputFolder"] as string;
        }
    }
}

还有我的 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Processors" type="Ideagen.PHE_PMEP_Processor_v2.Config.ProcessorsConfig, PHEPMEPProcessorv2"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <appSettings>
  </appSettings>

  <Processors>
    <Processor name="test" collectionFolder="c:\temp\phev2\collection" searchPattern="*.txt" failureFolder="c:\temp\phev2\failure" xmlOutputFolder="c:\temp\phev2\xmlout"></Processor>
  </Processors>
</configuration>

有什么线索吗?

【问题讨论】:

    标签: c# app-config configuration-files


    【解决方案1】:

    要让它像您在原始帖子中那样工作,您可以...

    <configuration>
        <configSections>
            <sectionGroup name="Processors">
                <section name="Processor" type="Ideagen.PHE_PMEP_Processor_v2.Config.ProcessorsConfig, PHEPMEPProcessorv2"/>
            </sectionGroup>
        </configSections>
        <Processors>
            <Processor name="test" collectionFolder="c:\temp\phev2\collection" searchPattern="*.txt" failureFolder="c:\temp\phev2\failure" xmlOutputFolder="c:\temp\phev2\xmlout"></Processor>
        </Processors>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      我们有它...

      要使上面的代码正常工作,我需要将“处理器”元素与另一个“处理器”元素一起包装在“处理器”部分中:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <configSections>
          <section name="Processors" type="Ideagen.PHE_PMEP_Processor_v2.Config.ProcessorsConfig, PHEPMEPProcessorv2"/>
        </configSections>
        <startup>
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
        </startup>
        <appSettings>
        </appSettings>
      
        <Processors>
          <Processors>
            <Processor name="test" collectionFolder="c:\temp\phev2\collection" searchPattern="*.txt" failureFolder="c:\temp\phev2\failure" xmlOutputFolder="c:\temp\phev2\xmlout"></Processor>
          </Processors>
        </Processors>
      </configuration>
      

      但是,为了更简洁的解决方案,我将 app.config 更改为:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <configSections>
          <section name="ProcessorConfig" type="Ideagen.PHE_PMEP_Processor_v2.Config.ProcessorsConfig, PHEPMEPProcessorv2"/>
        </configSections>
        <startup>
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
        </startup>
        <appSettings>
        </appSettings>
      
        <ProcessorConfig>
          <Processors>
            <Processor name="test" collectionFolder="c:\temp\phev2\collection" searchPattern="*.txt" failureFolder="c:\temp\phev2\failure" xmlOutputFolder="c:\temp\phev2\xmlout"></Processor>
          </Processors>
        </ProcessorConfig>
      </configuration>
      

      并将我的 ProcessorConfig 类更改如下:

      public static ProcessorsConfig GetConfig()
              {
                  return (ProcessorsConfig)ConfigurationManager.GetSection("ProcessorConfig") ?? new ProcessorsConfig();
              }
      

      星期五.. 新的星期一

      【讨论】:

        猜你喜欢
        • 2021-09-18
        • 1970-01-01
        • 2016-01-17
        • 2011-06-26
        • 2011-11-14
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        • 2016-12-17
        相关资源
        最近更新 更多