三.使用多个ConfigurationSource
当然了上面的解决方案我们可以看到,还是存在一些问题,首先使用external.config仅仅是把应用程序配置文件中的一部分分割出去放在了external.config中,我们仍然需要App.config文件来指定使用的是哪一个ConfigurationSource;其次两个应用程序块的配置信息放在了同一个文件中。这样我们就考虑对每一个应用程序块都能有一个配置文件,并且不使用App.config文件来指定使用哪一个,这样就要用编程的方法,可以使用静态的DatabaseFactory和ExceptionPolicy,如下面的例子所示:
data-filesource.config
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;
}
}
<?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中做完配置后再手动分割到不同的文件中。
四.使用.NET的configSource特性
.NET Framework2.0中System.Configuration允许对于应用程序配置文件中的每个配置区放到一个外部配置文件中,再用configSource的特性来指定各个配置区的外部文件。但是这种解决方案仍然是不能直接使用EntLibConfig.exe来配置,因为配置工具不能识别configSource。所以一个好的办法就是先使用EntLibConfig.exe在App.config中配置,最后再手动修改。这种方式对于使用Enterprise Library来说代码不变,如我们刚开始的例子所示,应用程序配置文件如下: data.config
exceptions.config 以上几种方案仅仅是给你一个参考,你可以在开发中根据实际情况选用其中的一种或者使用默认的方式。
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.
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<exceptionHandling configSource="exceptions.config" />
<connectionStrings configSource="data.config" />
</configuration>
connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;
Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
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>