以下内容以FastSocket为例子
<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>
configSections
节点下的子节点,看子节点的 name
属性 socketServer
,socketServer
对应在configuration
节点下的socketServer
然后 type
这个就是制定那个类型的类来处理 socketServer
节点的内容
public class SocketServerConfig : ConfigurationSection
{
[ConfigurationProperty("servers", IsRequired = true)]
public ServerCollection Servers
{
get { return this["servers"] as ServerCollection; }
}
}
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; }
}
}
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"]; }
}
}
var scoketServerConfig = ConfigurationManager.GetSection("socketServer") as SocketServerConfig;