<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Enterprise Library 2.0 技巧(1):如何使用外部配置文件

      摘要:我們知道在Enterprise Library1.1中對于每一個應(yīng)用程序塊都有一個對應(yīng)的配置文件,而在Enterprise Library2.0中卻把所有的配置信息都放在了應(yīng)用程序配置文件(App.configWeb.config)中,在2.0下,我們?nèi)绾问褂猛獠颗渲梦募咳绾螢槊總€應(yīng)用程序塊創(chuàng)建對應(yīng)的配置文件?

       

      主要內(nèi)容

      1.不使用外部配置文件

      2.使用不同的ConfigurationSource

      3.使用多個ConfigurationSource

      4.使用.NETconfigSource特性

       

      一.不使用外部配置文件

      我們先來看一個簡單的使用Enterprise Library的例子,在這個示例中,使用了企業(yè)庫的Data Access Application Block Excepiton Handling Application Block

      using System;

      using System.Collections.Generic;

      using System.Text;

      using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

      using Microsoft.Practices.EnterpriseLibrary.Data;

      using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

      namespace EntLibConfig
      {
          
      class Program
          
      {
              
      static void Main(string[] args)
              
      {
                  
      try
                  
      {
                      Database db 
      = DatabaseFactory.CreateDatabase("EntLibInstance");

                      db.ExecuteNonQuery(
      "ProcName");
                  }

                  
      catch (Exception ex)
                  
      {
                      
      if (ExceptionPolicy.HandleException(ex, "Event Policy"))

                          
      throw;
                  }

              }

          }

      }

      使用Enterprise Library Configuration配置之后,App.config文件如下:

      <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        
      <configSections>

          
      <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

          
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

        
      </configSections>

        
      <exceptionHandling>

          
      <exceptionPolicies>

            
      <add name="Event Policy">

              
      <exceptionTypes>

                
      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

                  postHandlingAction
      ="ThrowNewException" name="Exception">

                  
      <exceptionHandlers>

                    
      <add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                      type
      ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                      name
      ="Replace Handler" />

                  
      </exceptionHandlers>

                
      </add>

              
      </exceptionTypes>

            
      </add>

          
      </exceptionPolicies>

        
      </exceptionHandling>

        
      <dataConfiguration defaultDatabase="EntLibInstance" />

        
      <connectionStrings>

          
      <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"

            providerName
      ="System.Data.SqlClient" />

        
      </connectionStrings>

      </configuration>

      我們知道在EL1.1下,對于不同的應(yīng)用程序塊是放在了不同的配置文件中,而到了2.0中可以看到,所有的配置信息都放在了應(yīng)用程序配置文件中(App.config或者Web.config)。但是很多時候配置文件中有很多的信息也是我們自己手動添加的,如果這些混合在一起會顯得非常混亂,所以我們并不想把所有的配置信息都放在應(yīng)用程序配置文件中,該如何實現(xiàn)呢?Tom Hollander給了我們幾種可行的方案。

      二.使用不用的ConfigurationSource

      Enterprise Library2.0使用實現(xiàn)了IConfigurationSource接口的類來獲取配置信息,默認情況下將獲取SystemConfigurationSource,就像上面的例子,但是它允許我們配置應(yīng)用程序來使用不同的ConfigurationSource,下面來看一下具體的使用。

      EntLibConfig.exe打開配置文件后,在配置文件節(jié)點上選擇New | ConfigurationSources

















      并新建一個File Configuration Source,指定文件的路徑和文件名
      Configuration Source節(jié)點設(shè)置SelectSourceFile Configuration Source

      保存配置后,此時就有了兩個配置文件,App.configexternal.config,分別看一下這兩個文件中的內(nèi)容:

      App.config

      <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        
      <configSections>

          
      <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

        
      </configSections>

        
      <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">

          
      <sources>

            
      <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

              filePath
      ="D:\Visual Studio2005 Project\EntLibConfig\EntLibConfig\external.config" />

            
      <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

          
      </sources>

        
      </enterpriseLibrary.ConfigurationSource>

       
      </configuration>

      external.config

      <configuration>

          
      <configSections>

              
      <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

              
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

          
      </configSections>

          
      <exceptionHandling>

              
      <exceptionPolicies>

                  
      <add name="Event Policy">

                      
      <exceptionTypes>

                          
      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

                              postHandlingAction
      ="ThrowNewException" name="Exception">

                              
      <exceptionHandlers>

                                  
      <add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                                      type
      ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                                      name
      ="Replace Handler" />

                              
      </exceptionHandlers>

                          
      </add>

                      
      </exceptionTypes>

                  
      </add>

              
      </exceptionPolicies>

          
      </exceptionHandling>

          
      <dataConfiguration defaultDatabase="EntLibInstance" />

          
      <connectionStrings>

              
      <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"

                  providerName
      ="System.Data.SqlClient" />

          
      </connectionStrings>

      </configuration>

      三.使用多個ConfigurationSource

      當(dāng)然了上面的解決方案我們可以看到,還是存在一些問題,首先使用external.config僅僅是把應(yīng)用程序配置文件中的一部分分割出去放在了external.config中,我們?nèi)匀恍枰?span lang="EN-US">App.config文件來指定使用的是哪一個ConfigurationSource;其次兩個應(yīng)用程序塊的配置信息放在了同一個文件中。這樣我們就考慮對每一個應(yīng)用程序塊都能有一個配置文件,并且不使用App.config文件來指定使用哪一個,這樣就要用編程的方法,可以使用靜態(tài)的DatabaseFactoryExceptionPolicy,如下面的例子所示:

      static void Main(string[] args)
      {
          
      try
          
      {
              FileConfigurationSource dataSource 
      = new FileConfigurationSource("data-filesource.config");

              DatabaseProviderFactory dbFactory 
      = new DatabaseProviderFactory(dataSource);

              Database db 
      = dbFactory.Create("EntLibInstance");

              db.ExecuteNonQuery(
      "ProcName");

          }

          
      catch (Exception ex)
          
      {
              FileConfigurationSource exceptionsSource 
      = new FileConfigurationSource("exceptions-filesource.config");

              ExceptionPolicyFactory exceptionFactory 
      = new ExceptionPolicyFactory(exceptionsSource);

              ExceptionPolicyImpl exceptionPolicy 
      = exceptionFactory.Create("Event Policy");

              
      if (exceptionPolicy.HandleException(ex))

                  
      throw;
          }

      }

      data-filesource.config

      <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        
      <configSections>

          
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

        
      </configSections>

        
      <dataConfiguration defaultDatabase="EntLibInstance" />

        
      <connectionStrings>

          
      <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"

              providerName
      ="System.Data.SqlClient" />

        
      </connectionStrings>

      </configuration>

      exceptions.filesource.config

      <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        
      <configSections>

          
      <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

        
      </configSections>

        
      <exceptionHandling>

          
      <exceptionPolicies>

            
      <add name="Event Policy">

              
      <exceptionTypes>

                
      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

                    postHandlingAction
      ="ThrowNewException" name="Exception">

                  
      <exceptionHandlers>

                    
      <add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                        type
      ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                        name
      ="Replace Handler" />

                  
      </exceptionHandlers>

                
      </add>

              
      </exceptionTypes>

            
      </add>

          
      </exceptionPolicies>

        
      </exceptionHandling>

      </configuration>

      這種解決方案其實也是存在了很多的問題:首先是把配置文件名硬編碼到了程序了;第二,對于配置文件只能先在App.config中做完配置后再手動分割到不同的文件中。

      四.使用.NETconfigSource特性

      .NET Framework2.0System.Configuration允許對于應(yīng)用程序配置文件中的每個配置區(qū)放到一個外部配置文件中,再用configSource的特性來指定各個配置區(qū)的外部文件。但是這種解決方案仍然是不能直接使用EntLibConfig.exe來配置,因為配置工具不能識別configSource。所以一個好的辦法就是先使用EntLibConfig.exeApp.config中配置,最后再手動修改。這種方式對于使用Enterprise Library來說代碼不變,如我們剛開始的例子所示,應(yīng)用程序配置文件如下:

      <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        
      <configSections>

            
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

            
      <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

        
      </configSections>

        
      <exceptionHandling configSource="exceptions.config" />

        
      <connectionStrings configSource="data.config" />

      </configuration>

      data.config

      <?xml version="1.0" encoding="utf-8"?>

      <dataConfiguration defaultDatabase="EntLibInstance" />

      <connectionStrings>

        
      <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"

            providerName
      ="System.Data.SqlClient" />

      </connectionStrings>

      exceptions.config

      <?xml version="1.0" encoding="utf-8"?>

      <exceptionHandling>

        
      <exceptionPolicies>

          
      <add name="Event Policy">

            
      <exceptionTypes>

              
      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

                  postHandlingAction
      ="ThrowNewException" name="Exception">

                
      <exceptionHandlers>

                  
      <add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                      type
      ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                      name
      ="Replace Handler" />

                
      </exceptionHandlers>

              
      </add>

            
      </exceptionTypes>

          
      </add>

        
      </exceptionPolicies>

      </exceptionHandling>


      以上幾種方案僅僅是給你一個參考,你可以在開發(fā)中根據(jù)實際情況選用其中的一種或者使用默認的方式。

      [注:本系列很多可能來自于國外一些技術(shù)Blog,我將重新加以整理] 

      參考資料

        External configuration files in Enterprise Library for .NET Framework 2.0

      posted @ 2006-07-03 17:54  TerryLee  閱讀(17250)  評論(24)    收藏  舉報
      主站蜘蛛池模板: 91国内精品久久精品一本| 欧美亚洲综合成人A∨在线| 久久人人爽人人爽人人av| 麻豆亚州无矿码专区视频| 狠狠亚洲色一日本高清色| 人人人澡人人肉久久精品| 亚洲国产日韩一区三区| 51妺嘿嘿午夜福利| 久久综合九色综合97伊人| 四虎国产精品永久在线国在线| 不卡国产一区二区三区| 羞羞影院午夜男女爽爽免费视频| 二区中文字幕在线观看| 欧美性猛交xxxx乱大交丰满| 国产午夜亚洲精品不卡下载| 国产亚洲第一精品| 亚洲精品日本久久一区二区三区| 亚洲精品日本久久久中文字幕| 精品国产一区二区三区蜜臀| 中文字幕不卡在线播放| 国产精品高清一区二区三区| 国产精品久久蜜臀av| 亚洲va在线∨a天堂va欧美va| 午夜丰满少妇性开放视频| 国产精品一区在线蜜臀| 人妻中文字幕不卡精品| 亚洲av专区一区| 成人爽A毛片在线视频淮北| 蚌埠市| 久久青草国产精品一区| 国产女人水真多18毛片18精品 | 亚洲国语自产一区第二页| 怡红院一区二区三区在线| 97人妻天天爽夜夜爽二区| 久久亚洲av成人无码软件| 四虎影视库国产精品一区| 国产三级精品片| 久久羞羞色院精品全部免费| 国产精品久久久午夜夜伦鲁鲁| 亚洲人成亚洲人成在线观看| 久久亚洲精品情侣|