【问题标题】:Problems with custom configuration section handler自定义配置部分处理程序的问题
【发布时间】:2013-04-25 18:43:00
【问题描述】:

所以我对使用 app-config 文件完全陌生,我试图创建一个自定义配置处理程序,以便我可以从同一个键中读取多个值,我按照 Microsoft 网站上的文档进行操作,但我遇到了问题。

每次我尝试运行我的代码时都会抛出这个错误

“无法识别的属性‘数据类型’。请注意,属性名称区分大小写。(C:\Users\stephen.carmody\Desktop\FlatFileFactory - Copy\FlatFileFactory\bin\Debug\FlatFileFactory.vshost.exe.Config 第 21 行)"

它似乎只识别元素中的前两个值,第三个“数据类型”抛出错误

这是我的配置文件

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Configuration section-handler declaration area. -->
 <configSections>
  <sectionGroup name="propertyValuesGroup">
  <section
    name="propertyValues"
    type="FlatFileTestCaseAutomater.CustomConfigurationSectionHandler,FlatFileFactory"
    allowLocation="true"
    allowDefinition="Everywhere"
  />
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections>

<!-- Configuration section settings area. -->


<propertyValuesGroup>
<propertyValues>
  <cHeaderProperty name="txnNo" nullable="yes" datatype="int" maxlength="" />
</propertyValues>
</propertyValuesGroup>

</configuration>

这是我的自定义处理程序类:

namespace FlatFileTestCaseAutomater
{
    class CustomConfigurationSectionHandler : ConfigurationSection
    {
        [ConfigurationProperty("cHeaderProperty")]
        public CHeaderPropertyElement Property
    {
        get
        {
            return (CHeaderPropertyElement)this["cHeaderProperty"];
        }
        set
        { this["cHeaderProperty"] = value; }
    }
}


public class ClaimHeaderElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String Name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }

    [ConfigurationProperty("dataType", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String DataType
    {
        get
        {
            return (String)this["dataType"];
        }
        set
        {
            this["dataType"] = value;
        }
    }

    [ConfigurationProperty("maxLength", DefaultValue = int.MaxValue, IsRequired = false)]
   // [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 0)]
    public int MaxLength
    {
        get
        { return (int)this["maxLength"]; }
        set
        { this["maxLength"] = value; }
    }

}

}

这是调试期间发生中断的代码的sn-p:

  FlatFileTestCaseAutomater.CustomConfigurationSectionHandler config =
    (FlatFileTestCaseAutomater.CustomConfigurationSectionHandler)System.Configuration.ConfigurationManager.GetSection(
    "propertyValuesGroup/propertyValues");

我知道以前曾发布过类似的帖子,但我已经在这几个小时没有运气了

【问题讨论】:

  • 我可能错了,但你在配置中的名字似乎不是同一个“大小写”,它们都是小写的(数据类型和最大长度),粘贴在这里时打错了吗?
  • 和什么情况一样?方法?我尝试使它们相同,相同的结果

标签: c# app-config configurationsection custom-sections


【解决方案1】:

我可能是错的,但你在配置中的名字似乎不一样 'case',它们都是小写的(数据类型和最大长度),错字时 在这里粘贴?

属性确实区分大小写 - 它应该是(基于您的代码)

<cHeaderProperty name="txnNo" nullable="yes" dataType="int" maxLength="" />

【讨论】:

  • 那是个错误,我原来有他们匹配,仍然不工作。根据这个 [link]msdn.microsoft.com/en-us/library/2tw134k3.aspx[/link] 我应该能够调用这样的值 - config.Property.MaxLenth ,但是当我输入 config.Property 时 maxLength 不是可用选项。
猜你喜欢
  • 2010-10-20
  • 2015-06-23
  • 1970-01-01
  • 2012-05-11
  • 2011-05-25
  • 1970-01-01
  • 2014-02-08
  • 2013-01-21
  • 2011-05-04
相关资源
最近更新 更多