![](/img/trans.png)
[英]ConfigurationManager.GetSection(“sectionName”) throws ConfigurationErrorsException for custom section
[英]ConfigurationManager.GetSection doesn't return custom section
因为我的新项目具有复杂的配置结构,所以我正在学习如何将类ConfigurationManager与自定义配置一起使用。
为此,我以MSDN如何:以ConfigurationSection为例创建自定义配置节 。 此示例显示了一个类的配置部分,该部分聚集了其他可配置类。
MyProblem:ConfigurationManager.GetSection不返回任何节
我创建了一个ConsoleApplication。
更改了App.Config(始终复制),如下所示:
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- Configuration section-handler declaration area. --> <configSections> <sectionGroup name="pageAppearanceGroup"> <section name="pageAppearance" type="TryConfigManagement.PageAppearance" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> </configSections> <!-- Configuration section settings area. --> <pageAppearanceGroup> <pageAppearance remoteOnly="true"> <font name="TimeNewRoman" size="18"/> <color background="000000" foreground="FFFFFF"/> </pageAppearance> </pageAppearanceGroup> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/> </startup> </configuration>
请注意,我使用了MSDN中示例中的配置组。
我的源代码:
namespace TryConfigManagement
{
class Program
{
static void Main(string[] args)
{
string sectionName = "pageAppearanceGroup/pageAppearance";
object section = ConfigurationManager.GetSection(sectionName);
这段代码引发以下异常:
An error occurred creating the configuration section handler
for pageAppearanceGroup/pageAppearance:
Could not load type 'TryConfigManagement.PageAppearanceSection'
from assembly 'System.Configuration, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(C:\...\bin\Debug\TryConfigManagement.vshost.exe.config line 6)
尽管我完全按照我从StackOverflow得出的示例中所做的那样做,但是我应该添加程序集信息,大概如下:
class Program
{
static void Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly();
string sectionName = "pageAppearanceGroup/pageAppearance ," +
assembly.FullName;
object section = ConfigurationManager.GetSection(sectionName);
现在不再抛出异常,但是返回的值为null。
我认为这与配置文件的节名称中的类型有关。 但是,尝试使用其他值(例如添加完整的程序集名称)无济于事。
las,我在GetSection中找不到很多有关字符串参数的信息。
那么我的app.config或GetSection调用怎么了?
您可以尝试在配置中指定TryConfigManagement.PageAppearance类型所在的程序集:
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="TryConfigManagement.PageAppearance, TryConfigManagement"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
然后,不要在GetSection参数中包含程序集名称。
另外,删除您的节名称后的空格 。
Smiech关于区分大小写的评论有所帮助(谢谢Smiech!),但这不是完整的答案。
如果使用类ConfigurationSectionGroupCollection和ConfigurationSectionCollection,则比起“ pageAppearanceGroup / pageAppearance”这样的魔术字符串处理,读取和维护起来要容易得多。
该代码如下所示:
namespace MyNameSpace
{
class Program
{
static void Main(string[] args)
{
const string configGroupName = "pageAppearanceGroup";
const string configSectionName = "pageAppearance";
Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
ConfigurationSectionGroup configGroup = config.SectionGroups[configGroupName];
PageAppearanceSection pageAppearanceConfig = (PageAppearanceSection)configGroup.Sections[configSectionName];
在这些之后,我可以执行以下语句:
Console.WriteLine("PageAppearance property values:");
Console.WriteLine("RemoteOnly = " + pageAppearanceConfig.RemoteOnly);
Console.WriteLine("Font: Name = {0}, Size = {1}",
pageAppearanceConfig.Font.Name,
pageAppearanceConfig.Font.Size);
Console.WriteLine("Color: Foreground {0}, Background {1}",
pageAppearanceConfig.Color.Foreground,
pageAppearanceConfig.Color.Background);
注意:为了了解App.Config,我将名称空间更改为MyNameSpace。 程序集名称仍为TryConfigManagement
为了完整起见,App.Config的内容。 请注意在何处使用名称空间和程序集名称
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name ="pageAppearanceGroup">
<section name="pageAppearance"
type ="TryConfigManagement.PageAppearanceSection, TryConfigManagement"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<!-- Configuration section settings area. -->
<pageAppearanceGroup>
<pageAppearance>
<font name="TimeNewRoman" size="18"/>
<color background="000000" foreground="FFFFFF"/>
</pageAppearance>
</pageAppearanceGroup>
</configuration>
部分定义的字段:
现在,如果找到一种摆脱所有魔术弦的方法,我所要做的就是。 但这是另一回事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.