App.Config自定义配置部分问题

时间:2010-12-14 05:55:58

标签: c# asp.net configuration app-config configuration-files

我为我的应用程序创建了一个自定义配置部分。由于某种原因,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;
        }
    }
}

UrlFilter

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

1 个答案:

答案 0 :(得分:3)

发现问题:

Decyclone是正确的,错误实际上只是编译时警告。

真正的问题是我正在访问我的配置:

UrlFilterCollection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterCollection;

应该是这样的

UrlFilterSection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterSection;

谢谢FlipScript和Decyclone:)

更新:

我发现了如何删除编译时警告 - 我正在使用Visual Studio 2010.在创建自定义配置部分后,我使用工具栏中的“创建架构”按钮生成配置的架构文件。然后我将其保存到我的项目中,警告消失了。