读取多层配置 configSections
webconfig配置(必须在第一行)
<configuration> <configSections> <section name="RedisSetting" type="System.Configuration.NameValueSectionHandler" /> </configSections>
然后webconfig配置:
<RedisSetting> <add key="RedisTimeOut" value="120" /> <!--连接池"写"连接对象数,通常配置为小于并发数(宁大勿小原则)--> <add key="MaxWritePoolSize" value="10000" /> <!--连接池"读"连接对象数,通常配置为小于并发数(宁大勿小原则)--> <add key="MaxReadPoolSize" value="100000" /> <!--Redis服务地址配置,写服务器,多个则使用英文逗号隔开192.168.100.152--> <add key="RedisAddressWrite" value="127.0.0.1:6379" /> <!--Redis服务地址配置,读服务器,多个则使用英文逗号隔开--> <add key="RedisAddressRead" value="127.0.0.1:6379" /> <!--通用Redis服务地址配置,写服务器,多个则使用英文逗号隔开192.168.100.152--> <add key="CommonRedisAddressWrite" value="127.0.0.1:6379" /> <!--通用Redis服务地址配置,读服务器,多个则使用英文逗号隔开--> <add key="CommonRedisAddressRead" value="127.0.0.1:6379" /> </RedisSetting>
代码实现:
NameValueCollection nameValueCollection = (NameValueCollection)ConfigurationManager.GetSection("RedisSetting"); this.RedisAddressWrite = nameValueCollection["RedisAddressWrite"]; this.RedisAddressRead = nameValueCollection["RedisAddressRead"]; this.CommonRedisAddressWrite = nameValueCollection["CommonRedisAddressWrite"]; this.CommonRedisAddressRead = nameValueCollection["CommonRedisAddressRead"]; this.MaxWritePoolSize = Convert.ToInt32(nameValueCollection["MaxWritePoolSize"]); this.MaxReadPoolSize = Convert.ToInt32(nameValueCollection["MaxReadPoolSize"]); this.Timeout = Convert.ToInt32(nameValueCollection["RedisTimeOut"]);

浙公网安备 33010602011771号