2010-06-01 102 views
2

我們使用來自entlib 4.1的緩存和日誌記錄應用程序塊。我們希望將這兩者的配置保存在單獨的文件中。我們怎樣才能做到這一點?企業庫4.1的多個配置源?

它看起來像entlib總是使用selectedSource作爲它的配置。

我試過如下:

<?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=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" /> 

    </configSections> 

    <enterpriseLibrary.ConfigurationSource selectedSource="messagesCache">  
    <sources>  
     <add name="messagesCache" filePath="Configuration\\messagesCache.config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />  
     <add name="logging" filePath="Configuration\\logging.config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />  
    </sources> 

    </enterpriseLibrary.ConfigurationSource>  
</configuration> 

但是,這並不工作,因爲應用程序塊始終使用selectedSource屬性值。

回答

3

External configuration files in Enterprise Library for .NET Framework 2.0指出:

[...],同時可以配置多達 配置資源爲你使用,只有一個是「選擇」 工具是其中一個企業要 圖書館 會自動使用[...]

我所做的是使用configSource屬性:

<configuration> 
    <configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    <section name="validationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
    </configSections> 

    <loggingConfiguration configSource="logging.config"/> 
    <exceptionHandlingConfiguration configSource="exceptionHandling.config"/> 
    <dataConfiguration configSource="dataAccess.config"/> 
    <validationConfiguration configSource="validation.config"/> 
</configuration> 

它工作的很好,但缺點是,如果您使用配置工具編輯應用程序/ Web配置文件並保存配置,它將保存在應用程序/ Web配置文件中。

+0

***運行時段*** ** configSource的問題** – Kiquenet 2015-07-01 11:28:54