C# 将EnterpriseLibrary块配置拆分为多个文件

C# 将EnterpriseLibrary块配置拆分为多个文件,c#,configuration,enterprise-library,configuration-files,C#,Configuration,Enterprise Library,Configuration Files,是否可以将企业库块的配置拆分为多个文件 例如:我有三个程序集和一个托管项目。我想将每个程序集的entlib v6异常处理应用程序块(EHAB)配置存储在位于特定程序集中的单独配置文件中 宿主项目引用三个程序集: 组装 ehabX.config 装配 ehabY.config 装配 ehabZ.config 托管项目 使用ehabX.config 使用ehabY.config 使用ehabZ.config 我已经尝试了以下方法: 托管项目中的App.config: <?xml

是否可以将企业库块的配置拆分为多个文件

例如:我有三个程序集和一个托管项目。我想将每个程序集的entlib v6异常处理应用程序块(EHAB)配置存储在位于特定程序集中的单独配置文件中

宿主项目引用三个程序集:

组装

  • ehabX.config
装配

  • ehabY.config
装配

  • ehabZ.config
托管项目

  • 使用ehabX.config
  • 使用ehabY.config
  • 使用ehabZ.config
我已经尝试了以下方法:

托管项目中的App.config:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
  </configSections>

  <enterpriseLibrary.ConfigurationSource>
    <sources>
      <add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
      <add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
      <add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
    </sources>
    <redirectSections>
      <add sourceName="ehabX" name="exceptionHandling" />
      <add sourceName="ehabY" name="exceptionHandling" />
      <add sourceName="ehabZ" name="exceptionHandling" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow NotImplementedException">
        <exceptionTypes>
          <add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow ArgumentException">
        <exceptionTypes>
          <add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

</configuration>

ehabX.config:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
  </configSections>

  <enterpriseLibrary.ConfigurationSource>
    <sources>
      <add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
      <add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
      <add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
    </sources>
    <redirectSections>
      <add sourceName="ehabX" name="exceptionHandling" />
      <add sourceName="ehabY" name="exceptionHandling" />
      <add sourceName="ehabZ" name="exceptionHandling" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow NotImplementedException">
        <exceptionTypes>
          <add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow ArgumentException">
        <exceptionTypes>
          <add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

</configuration>

ehabY.config:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
  </configSections>

  <enterpriseLibrary.ConfigurationSource>
    <sources>
      <add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
      <add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
      <add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
    </sources>
    <redirectSections>
      <add sourceName="ehabX" name="exceptionHandling" />
      <add sourceName="ehabY" name="exceptionHandling" />
      <add sourceName="ehabZ" name="exceptionHandling" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow NotImplementedException">
        <exceptionTypes>
          <add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

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

  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Swallow ArgumentException">
        <exceptionTypes>
          <add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

</configuration>

省略了ehabZ.config

将EHAB与策略“Swallow NotImplementedException”一起使用效果很好。但如果我尝试使用ehabY.config中定义的策略“Swallow ArgumentException”,我会收到以下错误消息:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandlingException:找不到名为“Swallow ArgumentException”的策略。异常处理中止


有什么建议吗?

不幸的是,没有现成的方法可以让您将多个配置源合并到一个配置集中


我想你可以做你想做的,但你必须做一些编码。您需要读入相应的配置源,并创建一个自定义的
IConfigurationSource
,将它们全部合并。有一个MergeConfigurationSource的示例可以帮助您。

所以您想将配置拆分为多个文件,但在运行时将它们全部合并回一个配置集?是的,Tuzo,这就是我想做的