摘要:我们知道在Enterprise Library1.1中对于每一个应用程序块都有一个对应的配置文件,而在Enterprise Library2.0中却把所有的配置信息都放在了应用程序配置文件(App.configWeb.config)中,在2.0下,我们如何使用外部配置文件?如何为每个应用程序块创建对应的配置文件?
 
主要内容
1.不使用外部配置文件
2.使用不同的ConfigurationSource
3.使用多个ConfigurationSource
4.使用.NETconfigSource特性
 
一.不使用外部配置文件
我们先来看一个简单的使用Enterprise Library的例子,在这个示例中,使用了企业库的Data Access Application Block Excepiton Handling Application Block
Enterprise Library 2.0 技巧(1):如何使用外部配置文件using System;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using System.Collections.Generic;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using System.Text;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using Microsoft.Practices.EnterpriseLibrary.Data;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
namespace EntLibConfig
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
class Program
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
static void Main(string[] args)
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
try
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                Database db 
= DatabaseFactory.CreateDatabase("EntLibInstance");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                db.ExecuteNonQuery(
"ProcName");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            }

Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
catch (Exception ex)
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                
if (ExceptionPolicy.HandleException(ex, "Event Policy"))
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                    
throw;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            }

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

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

Enterprise Library 2.0 技巧(1):如何使用外部配置文件}
使用Enterprise Library Configuration配置之后,App.config文件如下:
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<add name="Event Policy">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            postHandlingAction
="ThrowNewException" name="Exception">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
<exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件              
<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"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                name
="Replace Handler" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
</exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
</exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<dataConfiguration defaultDatabase="EntLibInstance" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      providerName
="System.Data.SqlClient" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</configuration>
我们知道在EL1.1下,对于不同的应用程序块是放在了不同的配置文件中,而到了2.0中可以看到,所有的配置信息都放在了应用程序配置文件中(App.config或者Web.config)。但是很多时候配置文件中有很多的信息也是我们自己手动添加的,如果这些混合在一起会显得非常混乱,所以我们并不想把所有的配置信息都放在应用程序配置文件中,该如何实现呢?Tom Hollander给了我们几种可行的方案。
二.使用不用的ConfigurationSource
Enterprise Library2.0使用实现了IConfigurationSource接口的类来获取配置信息,默认情况下将获取SystemConfigurationSource,就像上面的例子,但是它允许我们配置应用程序来使用不同的ConfigurationSource,下面来看一下具体的使用。
EntLibConfig.exe打开配置文件后,在配置文件节点上选择New | ConfigurationSources
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
并新建一个File Configuration Source,指定文件的路径和文件名Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Configuration Source节点设置SelectSourceFile Configuration SourceEnterprise Library 2.0 技巧(1):如何使用外部配置文件
保存配置后,此时就有了两个配置文件,App.configexternal.config,分别看一下这两个文件中的内容:
App.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<sources>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<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"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        filePath
="D:\Visual Studio2005 Project\EntLibConfig\EntLibConfig\external.config" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<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" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</sources>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</enterpriseLibrary.ConfigurationSource>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件 
</configuration>
external.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
<add name="Event Policy">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                
<exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                    
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                        postHandlingAction
="ThrowNewException" name="Exception">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                        
<exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                            
<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"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                                type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                                name
="Replace Handler" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                        
</exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                    
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                
</exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
</exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<dataConfiguration defaultDatabase="EntLibInstance" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            providerName
="System.Data.SqlClient" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</configuration>
三.使用多个ConfigurationSource
当然了上面的解决方案我们可以看到,还是存在一些问题,首先使用external.config仅仅是把应用程序配置文件中的一部分分割出去放在了external.config中,我们仍然需要App.config文件来指定使用的是哪一个ConfigurationSource;其次两个应用程序块的配置信息放在了同一个文件中。这样我们就考虑对每一个应用程序块都能有一个配置文件,并且不使用App.config文件来指定使用哪一个,这样就要用编程的方法,可以使用静态的DatabaseFactoryExceptionPolicy,如下面的例子所示:
Enterprise Library 2.0 技巧(1):如何使用外部配置文件static void Main(string[] args)
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
try
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        FileConfigurationSource dataSource 
= new FileConfigurationSource("data-filesource.config");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        DatabaseProviderFactory dbFactory 
= new DatabaseProviderFactory(dataSource);
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        Database db 
= dbFactory.Create("EntLibInstance");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        db.ExecuteNonQuery(
"ProcName");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    }

Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
catch (Exception ex)
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
{
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        FileConfigurationSource exceptionsSource 
= new FileConfigurationSource("exceptions-filesource.config");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        ExceptionPolicyFactory exceptionFactory 
= new ExceptionPolicyFactory(exceptionsSource);
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        ExceptionPolicyImpl exceptionPolicy 
= exceptionFactory.Create("Event Policy");
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
if (exceptionPolicy.HandleException(ex))
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
throw;
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    }

Enterprise Library 2.0 技巧(1):如何使用外部配置文件}
data-filesource.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<dataConfiguration defaultDatabase="EntLibInstance" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        providerName
="System.Data.SqlClient" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</configuration>
exceptions.filesource.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<add name="Event Policy">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件              postHandlingAction
="ThrowNewException" name="Exception">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
<exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件              
<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"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                  type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                  name
="Replace Handler" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
</exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
</exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</configuration>
这种解决方案其实也是存在了很多的问题:首先是把配置文件名硬编码到了程序了;第二,对于配置文件只能先在App.config中做完配置后再手动分割到不同的文件中。
四.使用.NETconfigSource特性
.NET Framework2.0System.Configuration允许对于应用程序配置文件中的每个配置区放到一个外部配置文件中,再用configSource的特性来指定各个配置区的外部文件。但是这种解决方案仍然是不能直接使用EntLibConfig.exe来配置,因为配置工具不能识别configSource。所以一个好的办法就是先使用EntLibConfig.exeApp.config中配置,最后再手动修改。这种方式对于使用Enterprise Library来说代码不变,如我们刚开始的例子所示,应用程序配置文件如下:
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<configuration>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</configSections>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<exceptionHandling configSource="exceptions.config" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<connectionStrings configSource="data.config" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</configuration>
data.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<dataConfiguration defaultDatabase="EntLibInstance" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<connectionStrings>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      providerName
="System.Data.SqlClient" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</connectionStrings>
exceptions.config
Enterprise Library 2.0 技巧(1):如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
<exceptionHandling>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
<exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
<add name="Event Policy">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
<exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            postHandlingAction
="ThrowNewException" name="Exception">
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
<exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件            
<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"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件                name
="Replace Handler" />
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件          
</exceptionHandlers>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件        
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件      
</exceptionTypes>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件    
</add>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件  
</exceptionPolicies>
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
Enterprise Library 2.0 技巧(1):如何使用外部配置文件
</exceptionHandling>

以上几种方案仅仅是给你一个参考,你可以在开发中根据实际情况选用其中的一种或者使用默认的方式。
[注:本系列很多可能来自于国外一些技术Blog,我将重新加以整理] 

















本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67625 ,如需转载请自行联系原作者

相关文章:

  • 2022-02-27
  • 2021-08-28
  • 2021-07-29
  • 2021-11-08
  • 2021-10-25
  • 2022-12-23
  • 2021-10-11
猜你喜欢
  • 2022-02-12
  • 2021-06-02
  • 2021-06-09
  • 2021-07-06
相关资源
相似解决方案