簡體   English   中英

自定義ConfigurationSection與sectionGroup不起作用

[英]custom ConfigurationSection with sectionGroup not working

嘗試訪問ConnectionConfiguration始終返回默認值或0表示整數數據類型。 不會拋出任何錯誤,它只是無法獲取每個屬性的值。 請幫我解決這個問題。

我在控制台應用程序中有一個app.config文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="templates" >
      <section name="connections" type="Test.ConnectionConfiguration, Test" allowLocation="true" allowDefinition="Everywhere"/>
    </sectionGroup>
  </configSections>
  <templates>
    <connections allowed="true" port="8080">
      <internal name="local" size="1344"/>
      <external name="internet" value="http" />
    </connections>
  </templates>
</configuration>

課程

public class ConnectionConfiguration : ConfigurationSection
{
    [ConfigurationProperty("allowed", IsRequired = true)]
    public bool Allowed
    {
        get
        {
            return (bool)this["allowed"];
        }
        set
        {
            this["allowed"] = value;
        }
    }

    [ConfigurationProperty("port", IsRequired = true)]
    public int Port
    {
        get { return (int)this["port"]; }
        set { this["port"] = value; }
    }

    [ConfigurationProperty("internal")]
    public InternalElement Internal
    {
        get { return (InternalElement)this["internal"]; }
        set { this["internal"] = value; }
    }

    [ConfigurationProperty("external")]
    public ExternalElement External
    {
        get { return (ExternalElement)this["external"]; }
        set { this["external"] = value; }
    }
}
public class InternalElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "local", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("size", DefaultValue = "1234", IsRequired = false)]
    [IntegerValidator(ExcludeRange = false, MaxValue = 6000, MinValue = 1)]
    public int Size
    {
        get { return (int)this["size"]; }
        set { this["size"] = value; }
    }
}
public class ExternalElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "local", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("value", DefaultValue = "https", IsRequired = false)]
    public string Value
    {
        get { return (string)this["value"]; }
        set { this["value"] = value; }
    }
}

提前致謝!

ConnectionConfiguration config = new ConnectionConfiguration();
int test = config.Port; // it should be 8080 but it's 0

不幸的是,只是創建配置處理程序的新實例不會自動使用配置數據填充它。 你必須手動閱讀。 為了實現這一點,您可以在ConnectionConfiguration類中添加靜態屬性,例如:

public class ConnectionConfiguration : ConfigurationSection
{
    private static ConnectionConfiguration connections = ConfigurationManager.GetSection("templates/connections") as ConnectionConfiguration;
    public static ConnectionConfiguration Connections
    {
        get
        {
            return connections;
        }
    }
 // the rest is the same
}

在您的應用程序中,您現在可以訪問自定義配置部分,如下所示:

ConnectionConfiguration config = ConnectionConfiguration.Connections;
int test = config.Port; // it should now be 8080

使用此代碼獲取您要查找的部分`ConnectionConfiguration config =(ConnectionConfiguration)ConfigurationManager.GetSection(“templates / connections”);

https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM