服務1的描述信息,供IoC容器使用服務2的描述信息,供IoC容器使用。。。" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

類庫探源——System.Configuration 配置信息處理

系統 1699 0

按照MSDN描述?System.Configuration 命名空間 包含處理配置信息的類型

本篇文章主要兩方面的內容

1. 如何使用ConfigurationManager 讀取AppSetting和ConnectionStrings

2. 如何使用自定義 Section,我這里的自定義Section格式為

      
        <
      
      
        SectionName
      
      
        >
      
      
        <
      
      
        services
      
      
        >
      
      
        

    服務1的描述信息,供IoC容器使用

    服務2的描述信息,供IoC容器使用

    。。。

  
      
      
        </
      
      
        services
      
      
        >
      
      
        </
      
      
        SectionName
      
      
        >
      
    

?

其實如果讀者使用一些比較出名的IoC 框架(Unity)都會有配置管理,根本不用自定義Section,但是技多不壓身,多了解點東西總是沒錯的。

?

一、ConfigurationManager 類

提供對客戶端應用程序配置文件的訪問。無法繼承此類

命名空間: System.Configuration

程序集: System.Configuration.dll

繼承關系:

原型定義:public static class ConfigurationManager

由上面的定義可以看出ConfigurationManager是一個靜態類

?

靜態屬性:

1. ConfigurationManager.AppSettings ? ?  獲取當前應用程序默認配置的 AppSettingsSection 數據

原型定義:public static NameValueCollection AppSettings { get; }

從上面的定義可以看出 AppSetting 是一個 鍵值對

?

2. ConfigurationManager.ConnectionStrings ?獲取當前應用程序默認配置的 ConnectionStringsSection 數據

例子:

app.config

        
           1
        
        
          <?
        
        
          xml version="1.0"
        
        
          ?>
        
        
           2
        
        
          <
        
        
          configuration
        
        
          >
        
        
           3
        
        
          <
        
        
          startup
        
        
          >
        
        
           4
        
        
          <
        
        
          supportedRuntime 
        
        
          version
        
        
          ="v2.0.50727"
        
        
          />
        
        
           5
        
        
          </
        
        
          startup
        
        
          >
        
        
           6
        
        
           7
        
        
          <
        
        
          connectionStrings
        
        
          >
        
        
           8
        
        
          <
        
        
          add 
        
        
          name
        
        
          ="KKSEntities"
        
        
           connectionString
        
        
          ="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=XXX.XXX.XXX.XXX;initial catalog=KKS;user id=XXXX;password=KKKKK;multipleactiveresultsets=True;App=EntityFramework&quot;"
        
        
           providerName
        
        
          ="System.Data.EntityClient"
        
        
          />
        
        
           9
        
        
          </
        
        
          connectionStrings
        
        
          >
        
        
          10
        
        
          11
        
        
          <
        
        
          appSettings
        
        
          >
        
        
          12
        
        
          <
        
        
          add 
        
        
          key
        
        
          ="Aphasia"
        
        
           value
        
        
          ="www.cnblogs.com/Aphasia"
        
        
          />
        
        
          13
        
        
          </
        
        
          appSettings
        
        
          >
        
        
          14
        
        
          </
        
        
          configuration
        
        
          >
        
      
View Code

Program.cs

        
           1
        
        
          using
        
        
           System;


        
        
           2
        
        
          using
        
        
           System.Configuration;


        
        
           3
        
        
           4
        
        
          namespace
        
        
           ConsoleApplication1


        
        
           5
        
        
          {


        
        
           6
        
        
          class
        
        
           Program


        
        
           7
        
        
              {


        
        
           8
        
        
          static
        
        
          void
        
         Main(
        
          string
        
        
          [] args)


        
        
           9
        
        
                  {


        
        
          10
        
        
          string
        
         web = ConfigurationManager.AppSettings[
        
          "
        
        
          Aphasia
        
        
          "
        
        
          ];


        
        
          11
        
        
                      Console.WriteLine(web);


        
        
          12
        
        
          13
        
        
          string
        
         cnstr = ConfigurationManager.ConnectionStrings[
        
          "
        
        
          KKSEntities
        
        
          "
        
        
          ].ConnectionString;


        
        
          14
        
        
                      Console.WriteLine(cnstr);


        
        
          15
        
        
          16
        
        
                  }


        
        
          17
        
        
              }


        
        
          18
        
         }
      
View Code

效果:

?

二、 自定義Section

app.config

            
               1
            
            
              <?
            
            
              xml version="1.0"
            
            
              ?>
            
            
               2
            
            
              <
            
            
              configuration
            
            
              >
            
            
               3
            
            
              <
            
            
              configSections
            
            
              >
            
            
               4
            
            
              <
            
            
              section 
            
            
              name
            
            
              ="ServicesSection"
            
            
               type
            
            
              ="CustomDemo.ServicesSection,CustomDemo"
            
            
              />
            
            
               5
            
            
              </
            
            
              configSections
            
            
              >
            
            
               6
            
            
               7
            
            
              <
            
            
              ServicesSection
            
            
              >
            
            
               8
            
            
              <
            
            
              services
            
            
              >
            
            
               9
            
            
              <
            
            
              add 
            
            
              ServiceName
            
            
              ="XXX服務1"
            
            
               Impl
            
            
              ="XXX.Impl.XXXService1"
            
            
               Inter
            
            
              ="XXX.Inter.XXXInter1"
            
            
              />
            
            
              10
            
            
              <
            
            
              add 
            
            
              ServiceName
            
            
              ="XXX服務2"
            
            
               Impl
            
            
              ="XXX.Impl.XXXService2"
            
            
               Inter
            
            
              ="XXX.Inter.XXXInter2"
            
            
              />
            
            
              11
            
            
              <
            
            
              add 
            
            
              ServiceName
            
            
              ="XXX服務3"
            
            
               Impl
            
            
              ="XXX.Impl.XXXService3"
            
            
               Inter
            
            
              ="XXX.Inter.XXXInter3"
            
            
              />
            
            
              12
            
            
              </
            
            
              services
            
            
              >
            
            
              13
            
            
              </
            
            
              ServicesSection
            
            
              >
            
            
              14
            
            
              15
            
            
              <
            
            
              startup
            
            
              >
            
            
              16
            
            
              <
            
            
              supportedRuntime 
            
            
              version
            
            
              ="v2.0.50727"
            
            
              />
            
            
              17
            
            
              </
            
            
              startup
            
            
              >
            
            
              18
            
            
              </
            
            
              configuration
            
            
              >
            
          
View Code

Program.cs

        
           1
        
        
          using
        
        
           System;


        
        
           2
        
        
          using
        
        
           System.Configuration;


        
        
           3
        
        
           4
        
        
          namespace
        
        
           CustomDemo


        
        
           5
        
        
          {


        
        
           6
        
        
          class
        
        
           Program


        
        
           7
        
        
              {


        
        
           8
        
        
          static
        
        
          void
        
         Main(
        
          string
        
        
          [] args)


        
        
           9
        
        
                  {


        
        
          10
        
                     ServicesSection myServices = ConfigurationManager.GetSection(
        
          "
        
        
          ServicesSection
        
        
          "
        
        ) 
        
          as
        
        
           ServicesSection;


        
        
          11
        
        
          foreach
        
         (Service item 
        
          in
        
        
           myServices.ServiceItems)


        
        
          12
        
        
                      {


        
        
          13
        
                         Console.WriteLine(
        
          "
        
        
          ServiceName:{0} Impl:{1} Inter:{2}
        
        
          "
        
        
          , item.ServiceName, item.Impl, item.Inter);


        
        
          14
        
        
                      }


        
        
          15
        
        
                  }


        
        
          16
        
        
              }


        
        
          17
        
        
          18
        
        
          #region
        
        [自定義Section處理代碼]


        
          19
        
        
          public
        
        
          class
        
        
           Service : ConfigurationElement


        
        
          20
        
        
              {


        
        
          21
        
        
          #region
        
        [當遇不能識別的元素時不讓程序報錯]


        
          22
        
        
          protected
        
        
          override
        
        
          bool
        
         OnDeserializeUnrecognizedAttribute(
        
          string
        
         name, 
        
          string
        
        
           value)


        
        
          23
        
        
                  {


        
        
          24
        
        
          return
        
        
          true
        
        
          ;


        
        
          25
        
        
                  }


        
        
          26
        
        
          protected
        
        
          override
        
        
          bool
        
         OnDeserializeUnrecognizedElement(
        
          string
        
        
           elementName, System.Xml.XmlReader reader)


        
        
          27
        
        
                  {


        
        
          28
        
        
          return
        
        
          true
        
        
          ;


        
        
          29
        
        
                  }


        
        
          30
        
        
          #endregion
        
        
          31
        
        
          32
        
        
          #region
        
        [節點元素]


        
          33
        
                 [ConfigurationProperty(
        
          "
        
        
          ServiceName
        
        
          "
        
        , IsRequired = 
        
          true
        
        
          )]


        
        
          34
        
        
          public
        
        
          string
        
        
           ServiceName


        
        
          35
        
        
                  {


        
        
          36
        
        
          get
        
         { 
        
          return
        
        
          this
        
        [
        
          "
        
        
          ServiceName
        
        
          "
        
        
          ].ToString(); }


        
        
          37
        
        
                  }


        
        
          38
        
        
          39
        
                 [ConfigurationProperty(
        
          "
        
        
          Impl
        
        
          "
        
        , IsRequired = 
        
          true
        
        
          )]


        
        
          40
        
        
          public
        
        
          string
        
        
           Impl


        
        
          41
        
        
                  {


        
        
          42
        
        
          get
        
         { 
        
          return
        
        
          this
        
        [
        
          "
        
        
          Impl
        
        
          "
        
        
          ].ToString(); }


        
        
          43
        
        
                  }


        
        
          44
        
        
          45
        
                 [ConfigurationProperty(
        
          "
        
        
          Inter
        
        
          "
        
        , IsRequired = 
        
          true
        
        
          )]


        
        
          46
        
        
          public
        
        
          string
        
        
           Inter


        
        
          47
        
        
                  {


        
        
          48
        
        
          get
        
         { 
        
          return
        
        
          this
        
        [
        
          "
        
        
          Inter
        
        
          "
        
        
          ].ToString(); }


        
        
          49
        
        
                  }


        
        
          50
        
        
          #endregion
        
        
          51
        
        
              }


        
        
          52
        
        
          53
        
        
          public
        
        
          class
        
        
           Services : ConfigurationElementCollection


        
        
          54
        
        
              {


        
        
          55
        
        
          protected
        
        
          override
        
        
           ConfigurationElement CreateNewElement()


        
        
          56
        
        
                  {


        
        
          57
        
        
          return
        
        
          new
        
        
           Service();


        
        
          58
        
        
                  }


        
        
          59
        
        
          protected
        
        
          override
        
        
          object
        
        
           GetElementKey(ConfigurationElement element)


        
        
          60
        
        
                  {


        
        
          61
        
        
          return
        
        
           ((Service)element).ServiceName;


        
        
          62
        
        
                  }


        
        
          63
        
        
              }


        
        
          64
        
        
          65
        
        
          public
        
        
          class
        
        
           ServicesSection : ConfigurationSection


        
        
          66
        
        
              {


        
        
          67
        
                 [ConfigurationProperty(
        
          "
        
        
          services
        
        
          "
        
        , IsDefaultCollection = 
        
          false
        
        
          )]


        
        
          68
        
        
          public
        
         Services ServiceItems { 
        
          get
        
         { 
        
          return
        
         (Services)
        
          base
        
        [
        
          "
        
        
          services
        
        
          "
        
        
          ]; } }


        
        
          69
        
        
              }


        
        
          70
        
        
          #endregion
        
        
          71
        
         }
      
View Code

效果:

本小節代碼 下載

?

三、參考資料

1.? .net自定義configSections的5個示例

?

類庫探源——System.Configuration 配置信息處理


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 伊人精品 | 97成人资源站 | 五月婷婷国产 | 特黄日韩免费一区二区三区 | 亚洲欧美日韩成人网 | 日本免费久久 | 国产精品久久久久久福利 | 老司机福利在线播放 | 国产在线免 | 奇米影视奇奇米色狠狠色777 | 久久99久久成人免费播放 | 精品久久不卡 | α片毛片| 久久亚洲国产精品一区二区 | 这里只有精品99re在线 | 亚洲大片免费 | 99热久久国产精品免费观看 | 黄片毛片在线看 | 色插综合 | 国产精品二区页在线播放 | 四虎地址| 99热在这里只有精品 | 久久这里精品 | 久久图片| 天天综合天天综合 | 六月丁香婷婷综合 | 四虎午夜影院 | 热久久精品在线 | 奇米影视一区二区三区 | 国产精品乱码在线观看 | 综合婷婷 | 日韩黄色录像 | 午夜一级| 成人四虎影院 | 国产不卡福利 | 九九国产精品视频 | 国产伦精品一区二区三区视频小说 | 久久成人小视频 | 日韩伊人网 | 极品色综合 | 九九久久国产精品 |