App.Config自定义配置节问题

发布于 2024-10-07 08:15:22 字数 3025 浏览 2 评论 0原文

我已经为我的应用程序创建了一个自定义配置部分。由于某种原因,Visual Studio 2010 无法识别我的自定义属性。对于所有“添加”键,我收到与此类似的警告:

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:

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

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. 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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2024-10-14 08:15:22

发现问题:

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

真正的问题是我像这样访问我的配置:

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

当它应该像这样

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

谢谢FlipScript和Decyclone:)

更新:

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

Found the problem:

Decyclone was correct, errors were in fact just compile time warnings.

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 :)

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. I then saved this to my project and the warnings disappeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文