Custom Configuration. Help me, please, to spot the problem. ThankYou.

S

shapper

Hello,

On a custom Web.Config section I keep having the same error. This is
driving me crazy!!!

I keep having the following error:
Unrecognized attribute 'Type'. Note that attribute names are case-
sensitive. (C:\Documents and Settings\Miguel\My Documents\Website
\web.config line 42)

On code line:
object parent = ConfigurationManager.GetSection("projSettings");

And Web.Config line 42 is:
<add Key="AdSense" Type="Client" Access="one"/>

This is my test code to read an item of Google group in my Custom
Section:

bool found;
object parent =
ConfigurationManager.GetSection("projSettings");
ConfigurationElementCollection child =
(ConfigurationElementCollection)parent.GetType().GetProperty("projSettings").GetValue(parent,
null);
foreach (IKeyProvider element in child) {
if (element.Key == "AdSense") found = true;
}

This is my Web.Config:
...
<section
name="projSettings"
type="MyApp.Configuration.ProjSettingsSection"/>
</configSections>

<projSettings>
<Google>
<add Key="AdSense" Type="Client" Access="one"/>
<add Key="Analytics" Type="UserAccount" Access="two"/>
</Google>
</projSettings>

This is my custom configuration section class:

public class ProjSettingsSection : ConfigurationSection {
[ConfigurationProperty("Google")]
public GoogleCollection Google {
get { return this["Google"] as GoogleCollection; }
}
}
public class GoogleCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new Google();
}
protected override object GetElementKey(ConfigurationElement
element) {
return (element as Google).Key;
}
}
public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key { get; set; }
public string Type { get; set; }
public string Access { get; set; }
}
interface IKeyProvider {
string Key { get; set; }
}

I really tried everything I could think of for the past 2 weeks or
so ...
.... Every time I get free time I go back to this.

I get always the same error.

Could someone, please, help me out?

Thanks,
Miguel
 
J

John Saunders

shapper said:
Hello,

On a custom Web.Config section I keep having the same error. This is
driving me crazy!!!

I keep having the following error:
Unrecognized attribute 'Type'. Note that attribute names are case-
sensitive. (C:\Documents and Settings\Miguel\My Documents\Website
\web.config line 42)

On code line:
object parent = ConfigurationManager.GetSection("projSettings");

And Web.Config line 42 is:
<add Key="AdSense" Type="Client" Access="one"/>
....
This is my Web.Config: ....
<projSettings>
<Google>
<add Key="AdSense" Type="Client" Access="one"/>
<add Key="Analytics" Type="UserAccount" Access="two"/>
</Google>
</projSettings>
....
public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key { get; set; }
public string Type { get; set; }
public string Access { get; set; }
}

What happens if you use a ConfigurationProperty attribute on Type and
Access?
 
S

shapper

On a custom Web.Config section I keep having the same error. This is
driving me crazy!!!
I keep having the following error:
Unrecognized attribute 'Type'. Note that attribute names are case-
sensitive. (C:\Documents and Settings\Miguel\My Documents\Website
\web.config line 42)
On code line:
object parent = ConfigurationManager.GetSection("projSettings");
And Web.Config line 42 is:
 <add Key="AdSense" Type="Client" Access="one"/>
...
This is my Web.Config: ...
 <projSettings>
   <Google>
     <add Key="AdSense" Type="Client" Access="one"/>
     <add Key="Analytics" Type="UserAccount" Access="two"/>
   </Google>
 </projSettings>
...
 public class Google : ConfigurationElement, IKeyProvider {
   [ConfigurationProperty("Key", IsRequired = true)]
   public string Key { get; set; }
   public string Type { get; set; }
   public string Access { get; set; }
 }

What happens if you use a ConfigurationProperty attribute on Type and
Access?

Yes, I found the problem and it is exactly that!

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top