gpt4 book ai didi

c# - 自定义 C# 配置无法识别的元素异常

转载 作者:太空狗 更新时间:2023-10-29 17:47:01 25 4
gpt4 key购买 nike

我在 .NET 3.5 Windows 服务的 App.config 中有以下位:

<configSections>
<section name="ConfigurationServiceSection" type="SomeApp.Framework.Configuration.ConfigurationServiceSection, SomeApp.Framework"/>
</configSections>

<ConfigurationServiceSection configSource="ConfigSections\configurationServiceSection.config" />

我在 configurationServiceSection.config 中有这个:

<ConfigurationServiceSection>
<ConfigurationServices>
<ConfigurationService name="LocalConfig" host="localhost" port="40001" location="LON"/>
</ConfigurationServices>
</ConfigurationServiceSection>

代码如下:

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; }
}
}
}

当我尝试使用以下内容访问配置时:

var configurationServiceSection = (ConfigurationServiceSection)configuration.GetSection("ConfigurationServiceSection");

我得到这个异常:

Unrecognized element 'ConfigurationService'. (C:\Code\branches\ConfigurationService\SomeApp\Src\ConfigService\SomeApp.ConfigService.WindowsService\bin\Debug\ConfigSections\configurationServiceSection.config line 3)

在我看来一切都井井有条?

有什么想法吗?谢谢。

最佳答案

好的,深入了解:

我将“AddItemName”添加到 ConfigurationServiceSection 类,如下所示:

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 属性,如下所示:

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

protected override string ElementName
{
get { return "ConfigurationService"; }
}

关于c# - 自定义 C# 配置无法识别的元素异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7380627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com