asp.net C# config 自定义节点及自定义节点配置以独立文件存在

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gzy11/article/details/78406631

单位集体转Java了,还是放不下.NET,从02年至今。走过了无数个不眠之夜。还是一如既往的热爱.NET。不废话了。

今天本来写zookeeper的配置项及watch的东东,一不小心就写了下节点配置。代码很简单,写的过于繁杂并不代表多么厉害,化简为繁吧。zookeeper的稍后放出源码。

1、自定义节点配置。

2、自己定义节点配置的获取。

3、自定义节点配置以独立文件存在。

config  主要内容:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="baoxian">
      <section name="appSettings" type="System.Configuration.NameValueSectionHandler" />
    </sectionGroup>
  </configSections>
  <baoxian>
    <appSettings configSource="baoxianConfig\AppSettings.config" />
    <!--<appSettings>
      <add key="YinXin.BaoXian.UserOrderCount" value="/test/servicecenter-dev/services/{0}/providers"/>
    </appSettings>-->
  </baoxian>
</sectionGroup>

大量使用AppSettings这种形式是因为大家对这个使用的在熟悉不过了,获取的代码也很简单。写复杂了 华而不实,不实用。

AppSettings.config:内容

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="ZookeeperConnectString" value="192.168.145.3:2281,192.168.145.4:2281,192.168.145.7:228"/>
  <add key="UserOrderCount" value="/test/servicecenter-dev/services/{0}/providers"/>
  <add key="RegionCityRPC" value="/test/servicecenter-dev/services/{0}/providers"/>
</appSettings>


C#代码就一行 简单就是美

 private NameValueCollection GetConfig()
 {
     return (NameValueCollection)ConfigurationManager.GetSection("baoxian/appSettings");
 }
这样就跟经常使用的AppSettings 没啥区别了 ,AppSettings本质也是NameValueCollection

扫描二维码关注公众号,回复: 3059548 查看本文章
GetConfig()["ZookeeperConnectString"]




猜你喜欢

转载自blog.csdn.net/gzy11/article/details/78406631