App.Config 自定义配置部分问题
Posted
技术标签:
【中文标题】App.Config 自定义配置部分问题【英文标题】:App.Config Custom configuration section problem 【发布时间】:2011-05-25 02:12:59 【问题描述】:我为我的应用程序创建了一个自定义配置部分。出于某种原因,Visual Studio 2010 没有使用我的自定义属性。对于所有“添加”键,我都会收到与此类似的警告:
Could not find schema information for the element 'urlFilterSection'
配置文件:
<configSections>
<section name="urlFilterSection" type="BotFinderApp.Models.UrlFilterSection, BotFinder" />
</configSections>
<urlFilterSection>
<urlFilterCollection>
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
</urlFilterCollection>
</urlFilterSection>
UrlFilterSection:
namespace BotFinderApp.Models
public class UrlFilterSection : ConfigurationSection
public UrlFilterSection()
[ConfigurationProperty("urlFilterCollection", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(UrlFilterCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public UrlFilterCollection Urls
get
var urlsCollection = (UrlFilterCollection)base["urlFilterCollection"];
return urlsCollection;
UrlFilterCollection
namespace BotFinderApp.Models
public class UrlFilterCollection : ConfigurationElementCollection
public UrlFilterCollection()
protected override ConfigurationElement CreateNewElement()
return new UrlFilter();
protected override object GetElementKey(ConfigurationElement element)
return ((UrlFilter)element).Url;
网址过滤器
namespace BotFinderApp.Models
public class UrlFilter : ConfigurationElement
public UrlFilter()
[ConfigurationProperty("url", DefaultValue = "", IsRequired = true)]
public string Url
get return (string)this["url"];
set this["url"] = value;
[ConfigurationProperty("numberOfIpsToExtract", DefaultValue = "0", IsRequired = true)]
public int NumberOfIpsToExtract
get return (int)this["numberOfIpsToExtract"];
set this["numberOfIpsToExtract"] = value;
【问题讨论】:
你能在你的应用程序中使用它吗?我的意思是,它只是编译时警告还是你甚至不能在你的应用程序中使用它? UrlFilterCollection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterCollection;返回 null... 【参考方案1】:发现问题:
Decyclone 是正确的,错误实际上只是编译时警告。
真正的问题是我像这样访问我的配置:
UrlFilterCollection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterCollection;
什么时候应该是这样的
UrlFilterSection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterSection;
谢谢 FlipScript 和 Decyclone :)
更新:
我发现了如何删除编译时警告 - 我使用的是 Visual Studio 2010。创建自定义配置部分后,我使用了工具栏中的“创建架构”按钮,该按钮为配置生成架构文件。然后我将其保存到我的项目中,警告消失了。
【讨论】:
以上是关于App.Config 自定义配置部分问题的主要内容,如果未能解决你的问题,请参考以下文章