简体   繁体   English

读取ConfigurationSection

[英]Reading a ConfigurationSection

I'm writing a Windows service that needs some information from the app.config - I know I could use AppSettings , but I'm trying to understand config sections a little better. 我正在写一个Windows服务,它需要来自app.config的一些信息-我知道我可以使用AppSettings ,但是我想更好地理解配置部分。

I've modified my App.Config: 我已经修改了App.Config:

<configuration>
  <startup>
    <supportedRuntime sku=".NETFramework,Version=v4.0"
                      version="v4.0"/>
  </startup>

  <configSections>
    <section name="config"
             type="MyNamespace.MyConfigurationSection"/>
  </configSections>

  <config aNumber="1"
          aBoolean="false"/>

</configuration>

and created the following class: 并创建以下类:

public sealed class MyConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("aNumber", DefaultValue = 1, IsRequired = false)]
    public int Number
    {
        get { return (int) this["aNumber"]; }
        set { this["aNumber"] = value; }
    }

    [ConfigurationProperty("aBoolean", DefaultValue = false, IsRequired = false)]
    public bool Boolean
    {
        get { return (bool) this["aBoolean"]; }
        set { this["aBoolean"] = value; }
    }
}

and I'm trying to access it like this: 我正在尝试像这样访问它:

public MyConfigurationSection ConfigSection
{
    get
    {
        var configurationSection = Configuration.GetSection("config");
        return (MyConfigurationSection) configurationSection;
    }
}

private static Configuration Configuration
{
    get { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); }
}

Everything works fine until I get to the (MyConfigurationSection) configurationSection cast. 一切正常,直到我进入(MyConfigurationSection) configurationSection为止。 I'm getting back a DefaultSection from my call to GetSection() , and the cast fails. 我从对GetSection()调用中恢复了DefaultSection ,并且GetSection()失败。

Where have I gone astray? 我哪里迷路了?

Possibly, although didn't check, 可能,尽管没有检查,

type="MyNamespace.MyConfigurationSection"/>

Should be 应该

type="<Namespace.ClassName>,<Namespace>"/>

The catch here is, you have to use the type value as "Namespace.ClassName, Namespace" in section configuration in app.config and more importantly the configSections must be the first child of the root configuration element in your app.config 这里的问题是,您必须在app.config的节配置中使用类型值作为“ Namespace.ClassName,Namespace”,更重要的是configSections必须是app.config中根配置元素的第一个子级

Below configuration might work for you: 以下配置可能适合您:

<configuration>
      <!-- <configSections> is moved to first child of <configuration> -->
      <configSections>
        <section name="config"
                 type="MyNamespace.MyConfigurationSection, MyNamespace"/>
      </configSections>

      <config aNumber="1"
              aBoolean="false"/>

      <startup>
        <supportedRuntime sku=".NETFramework,Version=v4.0"
                          version="v4.0"/>
      </startup>
</configuration>

config is a reserved word and cannot be used for the name of a configuration section. config是保留字,不能用作配置节的名称。 You also shouldn't name a variable Boolean. 您也不应该将变量命名为Boolean。

Try this following example: 请尝试以下示例:

using System.Configuration;

namespace ConfigSectionSample
{
    internal class MyConfigurationSection : ConfigurationSection
    {
        public static MyConfigurationSection Current
        {
            get
            {
                return (MyConfigurationSection)ConfigurationManager.GetSection("myConfig");
            }
        }

        [ConfigurationProperty("aNumber", IsRequired = true)]
        public int Number
        {
            get
            {
                return (int)this["aNumber"];
            }
        }

        [ConfigurationProperty("aBoolean", IsRequired = true)]
        public bool Boolean
        {
            get
            {
                return (bool)this["aBoolean"];
            }
        }
    }
}

With the following app,config 使用以下应用程序,配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="myConfig" type="ConfigSectionSample.MyConfigurationSection, ConfigSectionSample"></section>
  </configSections>

  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

  <myConfig aNumber="1" aBoolean="false" />
</configuration>

And get the values like this: 并获得如下所示的值:

        Console.WriteLine("Number: " + MyConfigurationSection.Current.Number);
        Console.WriteLine("Boolean: " + MyConfigurationSection.Current.Boolean);
        Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM