.Net中Cofnig配置文件的正规写法

严琨
2023-12-01

以下内容以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;
 类似资料: