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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

類庫(kù)探源——System.Configuration 配置信息處理

系統(tǒng) 1768 0

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

本篇文章主要兩方面的內(nèi)容

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

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

      
        <
      
      
        SectionName
      
      
        >
      
      
        <
      
      
        services
      
      
        >
      
      
        

    服務(wù)1的描述信息,供IoC容器使用

    服務(wù)2的描述信息,供IoC容器使用

    。。。

  
      
      
        </
      
      
        services
      
      
        >
      
      
        </
      
      
        SectionName
      
      
        >
      
    

?

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

?

一、ConfigurationManager 類

提供對(duì)客戶端應(yīng)用程序配置文件的訪問(wèn)。無(wú)法繼承此類

命名空間: System.Configuration

程序集: System.Configuration.dll

繼承關(guān)系:

原型定義:public static class ConfigurationManager

由上面的定義可以看出ConfigurationManager是一個(gè)靜態(tài)類

?

靜態(tài)屬性:

1. ConfigurationManager.AppSettings ? ?  獲取當(dāng)前應(yīng)用程序默認(rèn)配置的 AppSettingsSection 數(shù)據(jù)

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

從上面的定義可以看出 AppSetting 是一個(gè) 鍵值對(duì)

?

2. ConfigurationManager.ConnectionStrings ?獲取當(dāng)前應(yīng)用程序默認(rèn)配置的 ConnectionStringsSection 數(shù)據(jù)

例子:

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服務(wù)1"
            
            
               Impl
            
            
              ="XXX.Impl.XXXService1"
            
            
               Inter
            
            
              ="XXX.Inter.XXXInter1"
            
            
              />
            
            
              10
            
            
              <
            
            
              add 
            
            
              ServiceName
            
            
              ="XXX服務(wù)2"
            
            
               Impl
            
            
              ="XXX.Impl.XXXService2"
            
            
               Inter
            
            
              ="XXX.Inter.XXXInter2"
            
            
              />
            
            
              11
            
            
              <
            
            
              add 
            
            
              ServiceName
            
            
              ="XXX服務(wù)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
        
        [當(dāng)遇不能識(shí)別的元素時(shí)不讓程序報(bào)錯(cuò)]


        
          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
        
        [節(jié)點(diǎn)元素]


        
          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

效果:

本小節(jié)代碼 下載

?

三、參考資料

1.? .net自定義configSections的5個(gè)示例

?

類庫(kù)探源——System.Configuration 配置信息處理


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 日本高清在线观看视频www | 一区二区在线不卡 | 久久亚洲国产成人亚 | 国产精品久久久久无码人妻精品 | 色噜噜狠狠色综合久 | 大看蕉a在线观看 | 在线视频三级 | 欧美激情久久久 | 一级黄色毛片播放 | 国产精品99久久久久久www | 亚洲一区二区在线播放 | 久久se精品一区精品二区 | 国产免费福利视频一区二区 | 精品久久久一二三区 | 九九51精品国产免费看 | 山岸逢花在线观看无删减 | 国产女主播喷出白浆视频 | 国产裸体bbb视频 | 日本免费精品视频 | 日本美女久久 | 日日夜夜天天人人 | 青青草原亚洲 | 无限看片在线版免费视频大全 | 精品一区二区高清在线观看 | 亚洲高清在线播放 | 欧美视频一区二免费视频 | 亚洲欧美国产精品久久久久久久 | 国产精品久久久久一区二区 | 久久观看免费视频 | 夜色伊人 | 国产精品美女久久久久久久久久久 | 久久精品免费 | 午夜不卡一区二区 | 日韩欧美福利视频 | 最新亚洲视频 | 九九99热久久精品在线9 | 国产sm主人调教女m视频 | 天天操天天操天天操 | 91福利小视频 | 香蕉视频在线观看免费国产婷婷 | 777色狠狠一区二区三区香蕉 |