摘要:我们知道在Enterprise Library1.1中对于每一个应用程序块都有一个对应的配置文件,而在Enterprise Library2.0中却把所有的配置信息都放在了应用程序配置文件(App.config或Web.config)中,在2.0下,我们如何使用外部配置文件?如何为每个应用程序块创建对应的配置文件?

主要内容

1.不使用外部配置文件

2.使用不同的ConfigurationSource

3.使用多个ConfigurationSource

4.使用.NET的configSource特性

一.不使用外部配置文件

我们先来看一个简单的使用Enterprise Library的例子,在这个示例中,使用了企业库的Data Access Application Block和 Excepiton Handling Application Block。

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using System;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using System.Collections.Generic;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using System.Text;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using Microsoft.Practices.EnterpriseLibrary.Data;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件namespace EntLibConfig
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 class Program
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 static void Main(string[] args)
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 try
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                Database db = DatabaseFactory.CreateDatabase("EntLibInstance");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                db.ExecuteNonQuery("ProcName");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件            }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 catch (Exception ex)
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 if (ExceptionPolicy.HandleException(ex, "Event Policy"))
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 throw;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件            }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件    }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件}

使用Enterprise Library Configuration配置之后,App.config文件如下:

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="Event Policy">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件            postHandlingAction="ThrowNewException" name="Exception">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                name="Replace Handler" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <dataConfiguration defaultDatabase="EntLibInstance" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件      providerName="System.Data.SqlClient" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</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 技巧 如何使用外部配置文件

并新建一个File Configuration Source,指定文件的路径和文件名(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
在Configuration Source节点设置SelectSource为File Configuration Source(转)Enterprise Library 2.0 技巧 如何使用外部配置文件

保存配置后,此时就有了两个配置文件,App.config和external.config,分别看一下这两个文件中的内容:

App.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <sources>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        filePath="D:\Visual Studio2005 Project\EntLibConfig\EntLibConfig\external.config" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </sources>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </enterpriseLibrary.ConfigurationSource>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configuration>

external.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="Event Policy">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                        postHandlingAction="ThrowNewException" name="Exception">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                                name="Replace Handler" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <dataConfiguration defaultDatabase="EntLibInstance" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件            providerName="System.Data.SqlClient" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</configuration>

三.使用多个ConfigurationSource

当然了上面的解决方案我们可以看到,还是存在一些问题,首先使用external.config仅仅是把应用程序配置文件中的一部分分割出去放在了external.config中,我们仍然需要App.config文件来指定使用的是哪一个ConfigurationSource;其次两个应用程序块的配置信息放在了同一个文件中。这样我们就考虑对每一个应用程序块都能有一个配置文件,并且不使用App.config文件来指定使用哪一个,这样就要用编程的方法,可以使用静态的DatabaseFactory和ExceptionPolicy,如下面的例子所示:

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件static void Main(string[] args)
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 try
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        FileConfigurationSource dataSource = new FileConfigurationSource("data-filesource.config");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(dataSource);
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        Database db = dbFactory.Create("EntLibInstance");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        db.ExecuteNonQuery("ProcName");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件    }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 catch (Exception ex)
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件(转)Enterprise Library 2.0 技巧 如何使用外部配置文件{
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        FileConfigurationSource exceptionsSource = new FileConfigurationSource("exceptions-filesource.config");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        ExceptionPolicyImpl exceptionPolicy = exceptionFactory.Create("Event Policy");
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 if (exceptionPolicy.HandleException(ex))
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 throw;
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件    }
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件}

data-filesource.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <dataConfiguration defaultDatabase="EntLibInstance" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件        providerName="System.Data.SqlClient" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</configuration>

exceptions.filesource.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="Event Policy">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件              postHandlingAction="ThrowNewException" name="Exception">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                  type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                  name="Replace Handler" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</configuration>

这种解决方案其实也是存在了很多的问题:首先是把配置文件名硬编码到了程序了;第二,对于配置文件只能先在App.config中做完配置后再手动分割到不同的文件中。

四.使用.NET的configSource特性

.NET Framework2.0中System.Configuration允许对于应用程序配置文件中的每个配置区放到一个外部配置文件中,再用configSource的特性来指定各个配置区的外部文件。但是这种解决方案仍然是不能直接使用EntLibConfig.exe来配置,因为配置工具不能识别configSource。所以一个好的办法就是先使用EntLibConfig.exe在App.config中配置,最后再手动修改。这种方式对于使用Enterprise Library来说代码不变,如我们刚开始的例子所示,应用程序配置文件如下:

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<configuration>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </configSections>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandling configSource="exceptions.config" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <connectionStrings configSource="data.config" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</configuration>

data.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<dataConfiguration defaultDatabase="EntLibInstance" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<connectionStrings>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件      providerName="System.Data.SqlClient" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</connectionStrings>

exceptions.config

(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<?xml version="1.0" encoding="utf-8"?>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件<exceptionHandling>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add name="Event Policy">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件            postHandlingAction="ThrowNewException" name="Exception">
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 <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 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件                name="Replace Handler" />
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionHandlers>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionTypes>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </add>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件 </exceptionPolicies>
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件
(转)Enterprise Library 2.0 技巧 如何使用外部配置文件</exceptionHandling>

以上几种方案仅仅是给你一个参考,你可以在开发中根据实际情况选用其中的一种或者使用默认的方式。

相关文章:

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