Enterprise Library 2.0 技巧(1):如何使用外部配置文件
摘要:我們知道在Enterprise Library1.1中對于每一個應(yīng)用程序塊都有一個對應(yīng)的配置文件,而在Enterprise Library2.0中卻把所有的配置信息都放在了應(yīng)用程序配置文件(App.config或Web.config)中,在2.0下,我們?nèi)绾问褂猛獠颗渲梦募咳绾螢槊總€應(yīng)用程序塊創(chuàng)建對應(yīng)的配置文件?
主要內(nèi)容
1.不使用外部配置文件
2.使用不同的ConfigurationSource
3.使用多個ConfigurationSource
4.使用.NET的configSource特性
一.不使用外部配置文件
我們先來看一個簡單的使用Enterprise Library的例子,在這個示例中,使用了企業(yè)庫的Data Access Application Block和 Excepiton Handling Application Block。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
namespace EntLibConfig
{
class Program
{
static void Main(string[] args)
{
try
{
Database db = DatabaseFactory.CreateDatabase("EntLibInstance");
db.ExecuteNonQuery("ProcName");
}
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "Event Policy"))
throw;
}
}
}
}使用Enterprise Library Configuration配置之后,App.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" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, 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>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>我們知道在EL1.1下,對于不同的應(yīng)用程序塊是放在了不同的配置文件中,而到了2.0中可以看到,所有的配置信息都放在了應(yīng)用程序配置文件中(App.config或者Web.config)。但是很多時候配置文件中有很多的信息也是我們自己手動添加的,如果這些混合在一起會顯得非常混亂,所以我們并不想把所有的配置信息都放在應(yīng)用程序配置文件中,該如何實現(xiàn)呢?Tom Hollander給了我們幾種可行的方案。
二.使用不用的ConfigurationSource
Enterprise Library2.0使用實現(xiàn)了IConfigurationSource接口的類來獲取配置信息,默認情況下將獲取SystemConfigurationSource,就像上面的例子,但是它允許我們配置應(yīng)用程序來使用不同的ConfigurationSource,下面來看一下具體的使用。
用EntLibConfig.exe打開配置文件后,在配置文件節(jié)點上選擇New | ConfigurationSources。

并新建一個File Configuration Source,指定文件的路徑和文件名
在Configuration Source節(jié)點設(shè)置SelectSource為File Configuration Source
保存配置后,此時就有了兩個配置文件,App.config和external.config,分別看一下這兩個文件中的內(nèi)容:
App.config
<?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=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<sources>
<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"
filePath="D:\Visual Studio2005 Project\EntLibConfig\EntLibConfig\external.config" />
<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" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>external.config
<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" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, 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>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>三.使用多個ConfigurationSource
當(dāng)然了上面的解決方案我們可以看到,還是存在一些問題,首先使用external.config僅僅是把應(yīng)用程序配置文件中的一部分分割出去放在了external.config中,我們?nèi)匀恍枰?span lang="EN-US">App.config文件來指定使用的是哪一個ConfigurationSource;其次兩個應(yīng)用程序塊的配置信息放在了同一個文件中。這樣我們就考慮對每一個應(yīng)用程序塊都能有一個配置文件,并且不使用App.config文件來指定使用哪一個,這樣就要用編程的方法,可以使用靜態(tài)的DatabaseFactory和ExceptionPolicy,如下面的例子所示:
static void Main(string[] args)
{
try
{
FileConfigurationSource dataSource = 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;
}
}data-filesource.config
<?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允許對于應(yīng)用程序配置文件中的每個配置區(qū)放到一個外部配置文件中,再用configSource的特性來指定各個配置區(qū)的外部文件。但是這種解決方案仍然是不能直接使用EntLibConfig.exe來配置,因為配置工具不能識別configSource。所以一個好的辦法就是先使用EntLibConfig.exe在App.config中配置,最后再手動修改。這種方式對于使用Enterprise Library來說代碼不變,如我們剛開始的例子所示,應(yīng)用程序配置文件如下:
<?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" />
<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 configSource="exceptions.config" />
<connectionStrings configSource="data.config" />
</configuration>data.config
<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>exceptions.config
<?xml version="1.0" encoding="utf-8"?>
<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>
以上幾種方案僅僅是給你一個參考,你可以在開發(fā)中根據(jù)實際情況選用其中的一種或者使用默認的方式。
參考資料
External configuration files in Enterprise Library for .NET Framework 2.0
Worktile,新一代簡單好用、體驗極致的團隊協(xié)同、項目管理工具,讓你和你的團隊隨時隨地一起工作。完全免費,現(xiàn)在就去了解一下吧。
https://worktile.com



浙公網(wǎng)安備 33010602011771號