ConfigurationSection ConfigurationManager.GetSection() 始终返回 null
我正在尝试学习如何使用 ConfigurationSection 类。我曾经使用 IConfigurationSectionHandler 但发布说它已被折旧。因此,作为一个好小伙子,我正在尝试“正确”的方式。我的问题是它总是返回 null。
我有一个控制台应用程序和一个 DLL。
class Program
{
static void Main(string[] args)
{
StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();
string value = section.Value;
}
}
应用程序配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ConfigSectionGroup">
<section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
</sectionGroup>
</configSections>
<ConfigSectionGroup>
<ConfigSection>
<test value="1" />
</ConfigSection>
</ConfigSectionGroup>
</configuration>
DLL 中的节处理程序:
namespace Controller
{
public class StandardConfigSectionHandler : ConfigurationSection
{
private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";
public static StandardConfigSectionHandler GetConfiguration()
{
object section = ConfigurationManager.GetSection(ConfigPath);
return section as StandardWcfConfigSectionHandler;
}
[ConfigurationProperty("value")]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}
无论我为“ConfigPath”尝试什么值,它都会返回 null,或者抛出错误,指出“test”是无法识别的元素。我尝试过的值:
- ConfigSectionGroup
- ConfigSectionGroup/
- ConfigSectionGroup/ConfigSection
- ConfigSectionGroup/ConfigSection/
- ConfigSectionGroup/ConfigSection/test
- ConfigSectionGroup/ConfigSection/test/
I am trying to learn how to use the ConfigurationSection class. I used to use the IConfigurationSectionHandler but released that it has been depreciated. So being a good lad I am trying the "correct" way. My problem is that it is always returning null.
I have a console app and a DLL.
class Program
{
static void Main(string[] args)
{
StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();
string value = section.Value;
}
}
app config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ConfigSectionGroup">
<section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
</sectionGroup>
</configSections>
<ConfigSectionGroup>
<ConfigSection>
<test value="1" />
</ConfigSection>
</ConfigSectionGroup>
</configuration>
section handler in DLL:
namespace Controller
{
public class StandardConfigSectionHandler : ConfigurationSection
{
private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";
public static StandardConfigSectionHandler GetConfiguration()
{
object section = ConfigurationManager.GetSection(ConfigPath);
return section as StandardWcfConfigSectionHandler;
}
[ConfigurationProperty("value")]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}
What ever values I try for the "ConfigPath" it will return null, or throw an error saying "test" is an unrecognised element. Values I tried:
- ConfigSectionGroup
- ConfigSectionGroup/
- ConfigSectionGroup/ConfigSection
- ConfigSectionGroup/ConfigSection/
- ConfigSectionGroup/ConfigSection/test
- ConfigSectionGroup/ConfigSection/test/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码有一些问题。
您总是在
GetConfiguration
方法中返回null
,但我假设这只是在问题中,而不是在您的实际代码中。更重要的是,
ConfigPath
值的格式不正确。您有一个尾部斜杠ConfigSectionGroup/ConfigSection/
,删除最后一个斜杠,它将能够找到该部分。最重要的是,您声明部分的方式配置系统将期望您的“值”存储在
ConfigSection
元素的属性中。像这样<前><代码>
>
所以,把它们放在一起:
要了解有关如何配置配置部分的更多信息,请参阅这个优秀的 MSDN 文档:如何:使用 ConfigurationSection 创建自定义配置部分。它还包含有关如何在 的子元素(例如测试元素)中存储配置值的信息。
There's a couple of things wrong with your code.
You're always returning
null
in yourGetConfiguration
method but I'm going to assume that's just in the question and not in your actual code.More importantly, the format of the
ConfigPath
value is incorrect. You have a trailing slashConfigSectionGroup/ConfigSection/
, remove the last slash and it'll be able to find the section.Most important, the way you've declared your section the configuration system will expect your "value" be stored in an attribute of your
ConfigSection
element. Like thisSo, putting it all together:
To read more about how you configure configuration sections please consult this excellent MSDN documentation: How to: Create Custom Configuration Sections Using ConfigurationSection. It also contains information about how to store configuration values in sub-elements of (like your test element).
我遇到了类似的问题:
我的配置文件:
我收到错误:
我发现了有趣的解决方案,我将类文件移到了一个单独的项目中(类型=类库,名称= SwBankConfigHelper)。我将其添加到参考并更改配置文件:
我的代码工作正常!
I was similar problem with:
My configuration file:
I got an error:
I was found interesting solution, I moved the class file in a separate project (type = Class Library, name = SwBankConfigHelper) . I added it to Reference and change configuration file:
And my code works fine!