Reading from custom configuration section not working in .NET
I'm trying to read in values from a custom configuration section in a .NET 4.0 app. However, when inspecting the values they always display the defaults and not the custom values as I would expect.
Here's my class:
using System;
using System.Configuration;
namespace TestNamespace
{
public class TestSection : ConfigurationSection
{
[ConfigurationProperty("applicationAccessID", IsRequired = true)]
public string ApplicationAccessID { get; set; }
[ConfigurationProperty("username", IsRequired = true)]
public Guid Username { get; set; }
[ConfigurationProperty("password", IsRequired = true)]
public Guid Password { get; set; }
}
}
I've added the following items to the config file:
<configuration>
<configSecti开发者_StackOverflow中文版ons>
<sectionGroup name="myCustomConfiguration">
<section name="testSection" type="TestNamespace.TestSection,MyAssembly"/>
</sectionGroup>
</configSections>
<myCustomConfiguration>
<myAuthenticationSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" />
</myCustomConfiguration>
...
This line returns an instance of TestSection, but it's not reading the custom values from the config:
ConfigurationManager.GetSection("myCustomConfiguration/myAuthenticationSection")
Any advice?
EDIT: Here is my updated config file after removing groups:
<configuration>
<configSections>
<section name="testSection" type="TestNamespace.TestSection,MyAssembly"/>
</configSections>
<myAuthenticationSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" />
...
Same issue...
You're using groups, so you need to implement a group handler too:
public class MySettingsConfigurationGroup : ConfigurationSectionGroup
{
}
There is lot more to it, take a look here: http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
Update:
public class TestSection : ConfigurationSection
{
private static TestSection _testSection
= ConfigurationManager.GetSection("TestSection") as TestSection;
public static TestSection Settings
{
get
{
return _testSection;
}
}
[ConfigurationProperty("applicationAccessID", IsRequired = true)]
public string ApplicationAccessID { get; set; }
[ConfigurationProperty("username", IsRequired = true)]
public Guid Username { get; set; }
[ConfigurationProperty("password", IsRequired = true)]
public Guid Password { get; set; }
}
string id = TestSection.Settings.applicationAccessID;
Also, the name needs to match:
<testSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" />
I don't know if this question is still valid, but anyway.
When dealing with ConfigurationElement
s you need to address the issue that a configuration element is a collection of values, so you're NOT reading the values.
I'd replace for something like this:
[ConfigurationProperty("applicationAccessID", IsRequired = true)]
public string ApplicationAccessID
{
get { return (string)this["applicationAccessID"]; }
set { this["applicationAccessID"] = value; }
}
[ConfigurationProperty("username", IsRequired = true)]
public Guid Username
{
get { return (Guid)this["username"]; }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public Guid Password
{
get { return (Guid)this["password"]; }
set { this["password"] = value; }
}
精彩评论