2010-12-14 4 views
5

J'ai créé une section de configuration personnalisée pour mon application. Pour une raison quelconque, Visual Studio 2010 ne décroche pas et de mes propriétés personnalisées. Je reçois des avertissements similaires à cela pour toutes les clés "add":App.Config Problème de section de configuration personnalisée

Could not find schema information for the element 'urlFilterSection' 

FICHIER DE CONFIGURATION:

<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

Pouvez-vous l'utiliser dans votre application? Je veux dire, est-ce juste compiler des avertissements de temps ou vous ne pouvez même pas l'utiliser dans votre application? – decyclone

+0

UrlFilterCollection serviceConfigSection = ConfigurationManager.GetSection ("urlFilterSection") en tant que UrlFilterCollection; renvoie la valeur null ... – timothyclifford

Répondre

3

Trouvé le problème:

Decyclone était correcte, les erreurs étaient en fait simplement compiler des avertissements de temps.

Le vrai problème est que j'accédait à ma configuration comme ceci:

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

quand il aurait été comme ça

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

Merci FlipScript et Decyclone :)

MISE À JOUR:

J'ai découvert comment supprimer les avertissements de temps de compilation - Je suis usin g Visual Studio 2010. Après avoir créé ma section de configuration personnalisée, j'ai utilisé le bouton "Créer un schéma" de la barre d'outils qui génère le fichier de schéma pour la configuration. J'ai ensuite sauvé ceci à mon projet et les avertissements ont disparu.

Questions connexes