企业库4.1的多个配置源?

时间:2010-06-01 11:01:46

标签: c# asp.net enterprise-library

我们使用entlib 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属性值。

1 个答案:

答案 0 :(得分: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配置文件中。