1. 程式人生 > >.Net中Cofnig配置檔案的正規寫法

.Net中Cofnig配置檔案的正規寫法

以下內容以FastSocket為例子

配置節點XML寫法

<configuration>

  <configSections>
    <section name="socketServer"
             type="Sodao.FastSocket.Server.Config.SocketServerConfig, FastSocket.Server"/>
  </configSections>

  <socketServer>
    <servers>
      <server
name="quickStart" port="1500" socketBufferSize="8192" messageBufferSize="8192" maxMessageSize="102400" maxConnections="20000" serviceType="Server.MyService, Server" protocol="commandLine"/>
</servers>
</socketServer> </configuration>

寫法分析

  1. configSections 節點下的子節點,看子節點的 name 屬性 socketServer ,socketServer對應在configuration
    節點下的socketServer

  2. 然後 type 這個就是制定那個型別的類來處理 socketServer 節點的內容

public class SocketServerConfig : ConfigurationSection
{
    [ConfigurationProperty("servers", IsRequired = true
)] public ServerCollection Servers { get { return this["servers"] as ServerCollection; } } }
  1. 處理Servers節點的類,ConfigurationElementCollection表示包含一個子元素集合的配置元素,
    ConfigurationCollectionAttribute 以宣告的方式指示 .NET Framework 建立配置元素集合的例項
[ConfigurationCollection(typeof(Server), AddItemName = "server")]
public class ServerCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new Server();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        var server = element as Server;
        return server.Name;
    }
    public Server this[int i]
    {
        get { return BaseGet(i) as Server; }
    }
}
  1. 處理Server節點的類,ConfigurationElement表示配置檔案中的配置元素,
    ConfigurationPropertyAttribute以宣告方式指示 .NET Framework,以例項化配置屬性
public class Server : ConfigurationElement
{
    /// <summary>
    /// 名稱
    /// </summary>
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
    }
    /// <summary>
    /// 埠號。
    /// </summary>
    [ConfigurationProperty("port", IsRequired = true)]
    public int Port
    {
        get { return (int)this["port"]; }
    }

    /// <summary>
    /// Socket Buffer Size
    /// 預設8192 bytes
    /// </summary>
    [ConfigurationProperty("socketBufferSize", IsRequired = false, DefaultValue = 8192)]
    public int SocketBufferSize
    {
        get { return (int)this["socketBufferSize"]; }
    }
    /// <summary>
    /// Message Buffer Size
    /// 預設1024 bytes
    /// </summary>
    [ConfigurationProperty("messageBufferSize", IsRequired = false, DefaultValue = 8192)]
    public int MessageBufferSize
    {
        get { return (int)this["messageBufferSize"]; }
    }
    /// <summary>
    /// max message size,
    /// 預設4MB
    /// </summary>
    [ConfigurationProperty("maxMessageSize", IsRequired = false, DefaultValue = 1024 * 1024 * 4)]
    public int MaxMessageSize
    {
        get { return (int)this["maxMessageSize"]; }
    }
    /// <summary>
    /// 最大連線數,預設2W
    /// </summary>
    [ConfigurationProperty("maxConnections", IsRequired = false, DefaultValue = 20000)]
    public int MaxConnections
    {
        get { return (int)this["maxConnections"]; }
    }

    /// <summary>
    /// ServiceType
    /// </summary>
    [ConfigurationProperty("serviceType", IsRequired = true)]
    public string ServiceType
    {
        get { return (string)this["serviceType"]; }
    }
    /// <summary>
    /// 協議, 預設命令列協議
    /// </summary>
    [ConfigurationProperty("protocol", IsRequired = false, DefaultValue = "commandLine")]
    public string Protocol
    {
        get { return (string)this["protocol"]; }
    }
}
  1. 最後呼叫的時候讀取
 var scoketServerConfig = ConfigurationManager.GetSection("socketServer") as SocketServerConfig;