.Net custom config section not loading
I am trying to create a custom config section for storing api settings by region. This is for a asp.net web api.
However I am unable to get my custom config section to work. When I run the unit test it says:
Message: System.Configuration.ConfigurationErrorsException : Unrecognized attribute 'id'".
public class MyApiConfigSection : ConfigurationSection
{
public static Lazy<MyApiConfigSection> Configuration = new Lazy<MyApiConfigSection>(() =>
ConfigurationManager.GetSection("myApi/myApi.regions") as MyApiConfigSection);
[ConfigurationProperty("myApiSettings")]
public MyApiElement MyApiSettings
{
get
{
return (MyApiElement)this["myApiSettings"];
}
set
{
this["myApiSettings"] = value;
}
}
}
public class MyApiElement : ConfigurationElement
{
[ConfigurationProperty("apiVersions", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ApiVersionsCollection))]
public ApiVersionsCollection ApiVersions
{
get
{
ApiVersionsCollection gtVersions =
(ApiVersionsCollection)base["apiVersions"];
return gtVersions;
}
set
{
ApiVersionsCollection apiVersions = value;
}
}
}
public class ApiVersionsCollection : ConfigurationElementCollection
{
public ApiVersionsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new ApiVersionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((ApiVersionElement)element).Id;
}
public ApiVersionElement this[int index]
{
get
{
return (ApiVersionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ApiVersionElement this[string name]
{
get
{
return (ApiVersionElement)BaseGet(name);
}
}
}
public class ApiVersionElement : ConfigurationElement
{
public ApiVersionElement() { }
public ApiVersionElement(string id, string apiKey, RegionUrlsCollection regionUrls)
{
this.Id = id;
this.ApiKey = apiKey;
this.RegionUrls = regionUrls;
}
[ConfigurationProperty("id", IsRequired = true, IsKey = true)]
public string Id
{
get { return this["id"] as string; }
set { this["id"] = value; }
}
[ConfigurationProperty("apiKey", IsRequired = true)]
public string ApiKey
{
get { return (string)this["apiKey"]; }
set { this["apiKey"] = value; }
}
[ConfigurationProperty("regionUrls", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(RegionUrlsCollection))]
public RegionUrlsCollection RegionUrls
{
get
{
RegionUrlsCollection regionUrls =
(RegionUrlsCollection)base["regionUrls"];
return regionUrls;
}
set
{
RegionUrlsCollection regionUrls = value;
}
}
}
public class RegionUrlsCollection : ConfigurationElementCollection
{
public RegionUrlsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new RegionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((RegionElement)element).Name;
}
public RegionElement this[int index]
{
get
{
return (RegionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public RegionElement this[string name]
{
get
{
return (RegionElement)BaseGet(name);
}
}
}
public class RegionElement : ConfigurationElement
{
public RegionElement() { }
public RegionElement(string name, string url)
{
this.Name = name;
this.Url = url;
}
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}
}
The config section in the App.config is given below:
Other Similar sections are loading correctly -the only difference is that there are 2 nested collections here.
<myApi>
<myApi.regions>
<myApiSettings>
<apiVersions id="1" apiKey="sample_api_key_1">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
<apiVersions id="2" apiKey="sample_api_key_2">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
</myApiSettings>
</myApi.regions>
</myApi>
c# asp.net configsection
add a comment |
I am trying to create a custom config section for storing api settings by region. This is for a asp.net web api.
However I am unable to get my custom config section to work. When I run the unit test it says:
Message: System.Configuration.ConfigurationErrorsException : Unrecognized attribute 'id'".
public class MyApiConfigSection : ConfigurationSection
{
public static Lazy<MyApiConfigSection> Configuration = new Lazy<MyApiConfigSection>(() =>
ConfigurationManager.GetSection("myApi/myApi.regions") as MyApiConfigSection);
[ConfigurationProperty("myApiSettings")]
public MyApiElement MyApiSettings
{
get
{
return (MyApiElement)this["myApiSettings"];
}
set
{
this["myApiSettings"] = value;
}
}
}
public class MyApiElement : ConfigurationElement
{
[ConfigurationProperty("apiVersions", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ApiVersionsCollection))]
public ApiVersionsCollection ApiVersions
{
get
{
ApiVersionsCollection gtVersions =
(ApiVersionsCollection)base["apiVersions"];
return gtVersions;
}
set
{
ApiVersionsCollection apiVersions = value;
}
}
}
public class ApiVersionsCollection : ConfigurationElementCollection
{
public ApiVersionsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new ApiVersionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((ApiVersionElement)element).Id;
}
public ApiVersionElement this[int index]
{
get
{
return (ApiVersionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ApiVersionElement this[string name]
{
get
{
return (ApiVersionElement)BaseGet(name);
}
}
}
public class ApiVersionElement : ConfigurationElement
{
public ApiVersionElement() { }
public ApiVersionElement(string id, string apiKey, RegionUrlsCollection regionUrls)
{
this.Id = id;
this.ApiKey = apiKey;
this.RegionUrls = regionUrls;
}
[ConfigurationProperty("id", IsRequired = true, IsKey = true)]
public string Id
{
get { return this["id"] as string; }
set { this["id"] = value; }
}
[ConfigurationProperty("apiKey", IsRequired = true)]
public string ApiKey
{
get { return (string)this["apiKey"]; }
set { this["apiKey"] = value; }
}
[ConfigurationProperty("regionUrls", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(RegionUrlsCollection))]
public RegionUrlsCollection RegionUrls
{
get
{
RegionUrlsCollection regionUrls =
(RegionUrlsCollection)base["regionUrls"];
return regionUrls;
}
set
{
RegionUrlsCollection regionUrls = value;
}
}
}
public class RegionUrlsCollection : ConfigurationElementCollection
{
public RegionUrlsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new RegionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((RegionElement)element).Name;
}
public RegionElement this[int index]
{
get
{
return (RegionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public RegionElement this[string name]
{
get
{
return (RegionElement)BaseGet(name);
}
}
}
public class RegionElement : ConfigurationElement
{
public RegionElement() { }
public RegionElement(string name, string url)
{
this.Name = name;
this.Url = url;
}
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}
}
The config section in the App.config is given below:
Other Similar sections are loading correctly -the only difference is that there are 2 nested collections here.
<myApi>
<myApi.regions>
<myApiSettings>
<apiVersions id="1" apiKey="sample_api_key_1">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
<apiVersions id="2" apiKey="sample_api_key_2">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
</myApiSettings>
</myApi.regions>
</myApi>
c# asp.net configsection
check the case error betweenidandId. I think it is applicable for all properties you specified.
– Pranesh Janarthanan
Nov 23 '18 at 7:01
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15
add a comment |
I am trying to create a custom config section for storing api settings by region. This is for a asp.net web api.
However I am unable to get my custom config section to work. When I run the unit test it says:
Message: System.Configuration.ConfigurationErrorsException : Unrecognized attribute 'id'".
public class MyApiConfigSection : ConfigurationSection
{
public static Lazy<MyApiConfigSection> Configuration = new Lazy<MyApiConfigSection>(() =>
ConfigurationManager.GetSection("myApi/myApi.regions") as MyApiConfigSection);
[ConfigurationProperty("myApiSettings")]
public MyApiElement MyApiSettings
{
get
{
return (MyApiElement)this["myApiSettings"];
}
set
{
this["myApiSettings"] = value;
}
}
}
public class MyApiElement : ConfigurationElement
{
[ConfigurationProperty("apiVersions", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ApiVersionsCollection))]
public ApiVersionsCollection ApiVersions
{
get
{
ApiVersionsCollection gtVersions =
(ApiVersionsCollection)base["apiVersions"];
return gtVersions;
}
set
{
ApiVersionsCollection apiVersions = value;
}
}
}
public class ApiVersionsCollection : ConfigurationElementCollection
{
public ApiVersionsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new ApiVersionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((ApiVersionElement)element).Id;
}
public ApiVersionElement this[int index]
{
get
{
return (ApiVersionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ApiVersionElement this[string name]
{
get
{
return (ApiVersionElement)BaseGet(name);
}
}
}
public class ApiVersionElement : ConfigurationElement
{
public ApiVersionElement() { }
public ApiVersionElement(string id, string apiKey, RegionUrlsCollection regionUrls)
{
this.Id = id;
this.ApiKey = apiKey;
this.RegionUrls = regionUrls;
}
[ConfigurationProperty("id", IsRequired = true, IsKey = true)]
public string Id
{
get { return this["id"] as string; }
set { this["id"] = value; }
}
[ConfigurationProperty("apiKey", IsRequired = true)]
public string ApiKey
{
get { return (string)this["apiKey"]; }
set { this["apiKey"] = value; }
}
[ConfigurationProperty("regionUrls", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(RegionUrlsCollection))]
public RegionUrlsCollection RegionUrls
{
get
{
RegionUrlsCollection regionUrls =
(RegionUrlsCollection)base["regionUrls"];
return regionUrls;
}
set
{
RegionUrlsCollection regionUrls = value;
}
}
}
public class RegionUrlsCollection : ConfigurationElementCollection
{
public RegionUrlsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new RegionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((RegionElement)element).Name;
}
public RegionElement this[int index]
{
get
{
return (RegionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public RegionElement this[string name]
{
get
{
return (RegionElement)BaseGet(name);
}
}
}
public class RegionElement : ConfigurationElement
{
public RegionElement() { }
public RegionElement(string name, string url)
{
this.Name = name;
this.Url = url;
}
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}
}
The config section in the App.config is given below:
Other Similar sections are loading correctly -the only difference is that there are 2 nested collections here.
<myApi>
<myApi.regions>
<myApiSettings>
<apiVersions id="1" apiKey="sample_api_key_1">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
<apiVersions id="2" apiKey="sample_api_key_2">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
</myApiSettings>
</myApi.regions>
</myApi>
c# asp.net configsection
I am trying to create a custom config section for storing api settings by region. This is for a asp.net web api.
However I am unable to get my custom config section to work. When I run the unit test it says:
Message: System.Configuration.ConfigurationErrorsException : Unrecognized attribute 'id'".
public class MyApiConfigSection : ConfigurationSection
{
public static Lazy<MyApiConfigSection> Configuration = new Lazy<MyApiConfigSection>(() =>
ConfigurationManager.GetSection("myApi/myApi.regions") as MyApiConfigSection);
[ConfigurationProperty("myApiSettings")]
public MyApiElement MyApiSettings
{
get
{
return (MyApiElement)this["myApiSettings"];
}
set
{
this["myApiSettings"] = value;
}
}
}
public class MyApiElement : ConfigurationElement
{
[ConfigurationProperty("apiVersions", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ApiVersionsCollection))]
public ApiVersionsCollection ApiVersions
{
get
{
ApiVersionsCollection gtVersions =
(ApiVersionsCollection)base["apiVersions"];
return gtVersions;
}
set
{
ApiVersionsCollection apiVersions = value;
}
}
}
public class ApiVersionsCollection : ConfigurationElementCollection
{
public ApiVersionsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new ApiVersionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((ApiVersionElement)element).Id;
}
public ApiVersionElement this[int index]
{
get
{
return (ApiVersionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ApiVersionElement this[string name]
{
get
{
return (ApiVersionElement)BaseGet(name);
}
}
}
public class ApiVersionElement : ConfigurationElement
{
public ApiVersionElement() { }
public ApiVersionElement(string id, string apiKey, RegionUrlsCollection regionUrls)
{
this.Id = id;
this.ApiKey = apiKey;
this.RegionUrls = regionUrls;
}
[ConfigurationProperty("id", IsRequired = true, IsKey = true)]
public string Id
{
get { return this["id"] as string; }
set { this["id"] = value; }
}
[ConfigurationProperty("apiKey", IsRequired = true)]
public string ApiKey
{
get { return (string)this["apiKey"]; }
set { this["apiKey"] = value; }
}
[ConfigurationProperty("regionUrls", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(RegionUrlsCollection))]
public RegionUrlsCollection RegionUrls
{
get
{
RegionUrlsCollection regionUrls =
(RegionUrlsCollection)base["regionUrls"];
return regionUrls;
}
set
{
RegionUrlsCollection regionUrls = value;
}
}
}
public class RegionUrlsCollection : ConfigurationElementCollection
{
public RegionUrlsCollection() { }
protected override ConfigurationElement CreateNewElement()
{
return new RegionElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((RegionElement)element).Name;
}
public RegionElement this[int index]
{
get
{
return (RegionElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public RegionElement this[string name]
{
get
{
return (RegionElement)BaseGet(name);
}
}
}
public class RegionElement : ConfigurationElement
{
public RegionElement() { }
public RegionElement(string name, string url)
{
this.Name = name;
this.Url = url;
}
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}
}
The config section in the App.config is given below:
Other Similar sections are loading correctly -the only difference is that there are 2 nested collections here.
<myApi>
<myApi.regions>
<myApiSettings>
<apiVersions id="1" apiKey="sample_api_key_1">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
<apiVersions id="2" apiKey="sample_api_key_2">
<regionUrls>
<region name="amer" url="" />
<region name="apac" url="" />
<region name="emea" url="" />
</regionUrls>
</apiVersions>
</myApiSettings>
</myApi.regions>
</myApi>
c# asp.net configsection
c# asp.net configsection
edited Nov 23 '18 at 9:35
JohnB
1,94511320
1,94511320
asked Nov 23 '18 at 6:56
BipindasBipindas
95210
95210
check the case error betweenidandId. I think it is applicable for all properties you specified.
– Pranesh Janarthanan
Nov 23 '18 at 7:01
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15
add a comment |
check the case error betweenidandId. I think it is applicable for all properties you specified.
– Pranesh Janarthanan
Nov 23 '18 at 7:01
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15
check the case error between
id and Id. I think it is applicable for all properties you specified.– Pranesh Janarthanan
Nov 23 '18 at 7:01
check the case error between
id and Id. I think it is applicable for all properties you specified.– Pranesh Janarthanan
Nov 23 '18 at 7:01
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441949%2fnet-custom-config-section-not-loading%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441949%2fnet-custom-config-section-not-loading%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
check the case error between
idandId. I think it is applicable for all properties you specified.– Pranesh Janarthanan
Nov 23 '18 at 7:01
Change the case where? Its lowercase on the config and I am reading it as lowercase.
– Bipindas
Nov 23 '18 at 7:15