简体   繁体   English

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

[英]App.Config Custom configuration section problem

I've created a custom config section for my application. 我为我的应用程序创建了一个自定义配置部分。 For some reason Visual Studio 2010 isn't picking up and of my custom properties. 由于某种原因,Visual Studio 2010没有拾取和我的自定义属性。 I'm getting warnings similar to this for all the "add" keys: 对于所有“添加”键,我收到与此类似的警告:

Could not find schema information for the element 'urlFilterSection'

CONFIG FILE: 配置文件:

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

Found the problem: 发现问题:

Decyclone was correct, errors were in fact just compile time warnings. Decyclone是正确的,错误实际上只是编译时警告。

The real problem was I was accessing my configuration like this: 真正的问题是我正在访问我的配置,如下所示:

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

when it should have been like this 应该是这样的

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

Thank you FlipScript and Decyclone :) 谢谢FlipScript和Decyclone :)

UPDATE: 更新:

I found out how to remove the compile time warnings - I'm using Visual Studio 2010. After creating my custom configuration section/s I used the "Create Schema" button from the toolbar which generates the schema file for the config. 我发现了如何删除编译时警告 - 我正在使用Visual Studio 2010.在创建自定义配置部分后,我使用工具栏中的“创建架构”按钮生成配置的架构文件。 I then saved this to my project and the warnings disappeared. 然后我将其保存到我的项目中,警告消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM