本文介绍了无法识别的元素异常,自定义的C#配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在App.config中的下列位的.NET 3.5的Windows服务:
I have the following bits in App.config for a .NET 3.5 Windows Service:
<configSections>
<section name="ConfigurationServiceSection" type="SomeApp.Framework.Configuration.ConfigurationServiceSection, SomeApp.Framework"/>
</configSections>
<ConfigurationServiceSection configSource="ConfigSections\configurationServiceSection.config" />
我在configurationServiceSection.config了这一点:
I've got this in configurationServiceSection.config:
<ConfigurationServiceSection>
<ConfigurationServices>
<ConfigurationService name="LocalConfig" host="localhost" port="40001" location="LON"/>
</ConfigurationServices>
</ConfigurationServiceSection>
和这里的code:
using System.Configuration;
namespace SomeApp.Framework.Configuration
{
public sealed class ConfigurationServiceSection : ConfigurationSection
{
[ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(ConfigurationServices))]
public ConfigurationServices ConfigurationServices
{
get
{
return (ConfigurationServices)base["ConfigurationServices"];
}
}
}
public sealed class ConfigurationServices : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ConfigurationService();
}
protected override object GetElementKey(ConfigurationElement element)
{
ConfigurationService configService = (ConfigurationService) element;
return configService.Name;
}
}
public sealed class ConfigurationService : ConfigurationElement
{
/// <summary>
/// name
/// </summary>
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
/// <summary>
/// host
/// </summary>
[ConfigurationProperty("host", IsKey = false, IsRequired = true)]
public string Host
{
get { return (string)this["host"]; }
set { this["host"] = value; }
}
/// <summary>
/// port
/// </summary>
[ConfigurationProperty("port", IsKey = false, IsRequired = true)]
public string Port
{
get { return (string)this["port"]; }
set { this["port"] = value; }
}
/// <summary>
/// location
/// </summary>
[ConfigurationProperty("location", IsKey = false, IsRequired = true)]
public string Location
{
get { return (string)this["location"]; }
set { this["location"] = value; }
}
}
}
当我尝试访问配置为以下内容:
When I try to access the config with the following:
var configurationServiceSection = (ConfigurationServiceSection)configuration.GetSection("ConfigurationServiceSection");
我得到这个异常:
I get this exception:
Unrecognized element 'ConfigurationService'. (C:\Code\branches\ConfigurationService\SomeApp\Src\ConfigService\SomeApp.ConfigService.WindowsService\bin\Debug\ConfigSections\configurationServiceSection.config line 3)
一切看起来都为了我吗?
Everything looks in order to me?
任何想法吗?谢谢你。
推荐答案
确定了到这条底线:
我说'AddItemName到ConfigurationServiceSection类,按如下:
I added 'AddItemName' to the ConfigurationServiceSection class, as per below:
public sealed class ConfigurationServiceSection : ConfigurationSection
{
[ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(ConfigurationServices), AddItemName = "ConfigurationService")]
public ConfigurationServices ConfigurationServices
{
get
{
return (ConfigurationServices)base["ConfigurationServices"];
}
}
}
另一种方法是覆盖CollectionType和的ElementName属性,按如下:
Another alternative was to override the CollectionType and ElementName properties, as per below:
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "ConfigurationService"; }
}
这篇关于无法识别的元素异常,自定义的C#配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!