所以我创建了一个自定义部分处理程序来读取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/");
答案 0 :(得分:1)
你得到一个null因为段路径错误,它应该是:
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。我将它留在“添加”中,因为我在属性中添加了添加:
<!-- 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>