【问题标题】:App-config , custom section handler object returning as nullApp-config ,自定义部分处理程序对象返回为 null
【发布时间】:2014-01-12 11:03:35
【问题描述】:

所以我创建了一个自定义部分处理程序来从 app-config 文件中读取信息,但是当我尝试运行它时,我收到以下错误:对象引用未设置为对象的实例。

我已经尝试解决这个问题两天了,但没有成功

这是我的代码:App - config

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

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

  <propertyValuesGroup>

    <propertyValues>
      <property>
        <clear/>
      <claimHeader name="txnNo" nullable="yes" dataType="int" maxLength="20" />
      <claimHeader name="batchNo" nullable="yes" dataType="string" maxLength="20" />
      </property>
    </propertyValues>

  </propertyValuesGroup>


</configuration>

我的自定义部分处理程序:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace FlatFileTestCaseAutomater
{
    class ClaimHeaderSection : ConfigurationSection
    {

        [ConfigurationProperty("claimHeader")]
        public ClaimHeaderElement ClaimHeaderProperty
        {
            get
            {
                return (ClaimHeaderElement)this["claimHeader"];
            }
            set
            { this["claimHeader"] = value; }
        }
    }





    public class ClaimHeaderElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
        public String Name
        {
            get
            {
                return (String)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("nullable", DefaultValue = "yes", IsRequired = true)]
        public String Nullable
        {
            get
            {
                return (String)this["nullable"];
            }
            set
            {
                this["nullable"] = value;
            }
        }

        [ConfigurationProperty("dataType", DefaultValue = "", IsRequired = true)]
        public String DataType
        {
            get
            {
                return (String)this["dataType"];
            }
            set
            {
                this["dataType"] = value;
            }
        }

        [ConfigurationProperty("maxLength", DefaultValue = "", IsRequired = true)]
        public string MaxLength
        {
            get
            { return (string)this["maxLength"]; }
            set
            { this["maxLength"] = value; }
        }

    }

}

并调用对象:

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

【问题讨论】:

  • 你没有提到编程语言。它看起来像 C#。假设就是这样,我建议您删除现有标记之一,并将其​​替换为 C# 标记。这将大大提高您的问题的知名度,以及快速回答问题的机会。

标签: c# app-config nullreferenceexception configurationsection


【解决方案1】:

你得到一个空值,因为部分路径错误,它应该是:

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

但是现在您已经设计了自定义配置部分,如果您放置了有效路径,您将收到 ConfigurationErrorsException 并显示消息“无法识别的元素'属性'。”

根据您发布的配置,您需要一个 ClaimHeaderElement 的集合,但您没有在自定义部分中定义 ConfigurationElementCollection,而是定义了一个 ConfigurationElement。

为了工作,您应该实现一个 ConfigurationElementCollection,如下所示:

public class ClaimHeaderCollection : ConfigurationElementCollection 
{

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

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

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }
}

现在您必须在您的自定义部分中添加集合:

public class ClaimHeaderSection : ConfigurationSection
{
    [ConfigurationProperty("property", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(ClaimHeaderCollection), 
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public ClaimHeaderCollection ClaimHeaders
    {
        get
        {
            return (ClaimHeaderCollection)this["property"];
        }
        set
        { this["property"] = value; }
    }

    public static ClaimHeaderSection GetConfiguration()
    {
        return GetConfiguration("propertyValuesGroup/propertyValues");
    }

    public static ClaimHeaderSection GetConfiguration(string section) 
    {
        return ConfigurationManager.GetSection(section) as ClaimHeaderSection;
    }
}

请注意,我添加了两个名为 GetConfiguration 的静态方法,这将更容易获取配置部分:

ClaimHeaderSection config = ClaimHeaderSection.GetConfiguration();

在您的配置中,您应该将配置元素的名称从 claimHeader 更改为 add,如果您希望将 xml 节点称为 claimHeader 而不是 add,您应该将 ConfigurationCollection 属性中的 AddItemName 值从 add 更改为 claimHeader。我将把它留在“add”中,因为我把 add 放在了属性中:

<!-- Configuration section settings area. -->
<propertyValuesGroup>
  <propertyValues>
    <property>
      <clear/>
      <add name="txnNo" nullable="yes" dataType="int" maxLength="20" />
      <add name="batchNo" nullable="yes" dataType="string" maxLength="20" />
    </property>
  </propertyValues>
</propertyValuesGroup>

【讨论】:

  • 感谢您的回复,所以这是一个明显的错误,没有使用 configurationElementCollection,我对此仍然完全陌生,我尝试实现您的代码,但我仍然收到 nullReferenceException?
  • 我只是尝试它对我有用,我编辑了答案,因为我忘记在配置中进行一些更改,但这不会给你抛出空引用异常......如果你想要我可以发一个链接让你下载我刚刚做的小测试,它正在工作,让我知道。
  • 谢谢,我搞定了,我最后没有使用你的具体代码,但你让我走上了正轨!感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 2015-06-23
  • 2010-10-20
  • 1970-01-01
相关资源
最近更新 更多