[Enterprise Library]改变 Configuration File为其他配置文件

博客围绕让BookingUIP.dll引用的Application Block默认读取BookingUIP.dll.config而非其他配置文件展开。作者在使用不同程序调用BookingUIP.dll时遇到配置文件引用问题,尝试在ConfigurationManager内增加函数修改配置文件名,但认为并非最佳办法,还给出相关代码及不同测试结果。

标题:改变 Configuration File为其他配置文件
版本:Enterprise Library June 2005

问题:我知道 ConfigurationManager 缺省使用的配置文件是应用程序配置文件。假设我做了个dll,名字是BookingUIP.dll,它使用ConfigurationManager和Tracer(Logging Block内的对象)来读取参数和记录跟踪信息。如果我做一个WindowsFormsUI.exe来调用BookingUIP.dll,那么BookingUIP.dll缺省会读取WindowsFormsUI.exe.config配置文件。由于WindowsFormsUI.exe.config是我控制的,这不会出现什么问题。

但是,当我用NUnit 的nunit-gui.exe来调用 BookingUIP.dll的时候,BookingUIP.dll缺省会使用nunit-gui.exe.config来作为自己的配置文件。

现在,我的问题时,如何使BookingUIP.dll引用的所有Application Block,都缺省读取 BookingUIP.dll.config,而不是nunit-gui.exe.config或者WindowsFormsUI.exe.config。

我找来找去,发现没有办法,只好在ConfigurationManager内增加个SetConfigurationFile函数,把BookingUIP.dll.config文件全路径传给它,并且修改ConfigurationManager的缺省构造函数,让它使用我传入的配置文件名。

当然,这肯定不是最好的办法,不知道各位高手有什么好办法?
***********************************相关代码*********************
**********************************
1.BookingUIP.dll
我的BookingUIP.dll是从 BookingUIP.cs生成的,代码很简单:

None.gifnamespace BookingUIP 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
public class BEClass1 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
public  Microsoft.Practices.EnterpriseLibrary.Data.Database GetDatabase() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
return Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 

当然,编译成BookingUIP.dll的时候要引用EntLlib的Common,Configuration,Data 3个dll.

2.BookingUIP.dll.config和dataconfiguration.config
然后使用EntLibConfig.exe生成BookingUIP.dll.config和dataconfiguration.config.

3.WinFormApp1.exe
再做一个简单的WinForm程序,来调用 BEClass1的 GetDatabase方法:
WinFormApp1.cs:        

None.gif    private void btnGetDatabase_Click(object sender, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
string msg = null
InBlock.gif            BookingUIP.BEClass1 be 
= new BookingUIP.BEClass1(); 
InBlock.gif            Microsoft.Practices.EnterpriseLibrary.Data.Database db 
= null
InBlock.gif            
// 
InBlock.gif
            msg = "Current Configuration File is " + Environment.NewLine + Environment.NewLine + 
InBlock.gif                Microsoft.Practices.EnterpriseLibrary.Configuration.ConfigurationManager.GetCurrentContext().ConfigurationFile 
+ 
InBlock.gif                Environment.NewLine; 
InBlock.gif            MessageBox.Show(msg); 
InBlock.gif
InBlock.gif            msg 
= null
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                db 
= be.GetDatabase(); 
InBlock.gif                msg 
= "GetDatabase ok!"
InBlock.gif                MessageBox.Show(msg); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch (Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                msg 
= ex.Message; 
InBlock.gif                MessageBox.Show(msg, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }

None.gif

 从WinFormApp1.cs生成WinFormApp1.exe,要引用EntLlib的Common,Configuration,Data 3个dll.

4.测试结果
4.1 如果 WinFormApp1.exe.config不存在,那么btnGetDatabase_Click会报告
---------------------------
Current Configuration File is C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Config\machine.config
---------------------------

然后db = be.GetDatabase();失败:
---------------------------
Invalid section name. The section 'dataConfiguration' does not exist in the requested configuration file 'G:\..\WinFormApp1.exe.config' or the file 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Config\machine.config'. Make sure that the 'enterpriseLibrary.configurationSections' configuration section exists in one of the files and that the section 'dataConfiguration' is defined.
---------------------------


4.2如果WinFormApp1.exe.config存在,但是没有dataConfiguration配置块,那么btnGetDatabase_Click会报告
---------------------------
Current Configuration File is G:\..\WinFormApp1.exe.config
---------------------------

然后db = be.GetDatabase();失败:
---------------------------
Invalid section name. The section 'dataConfiguration' does not exist in the requested configuration file 'G:\..\WinFormApp1.exe.config' or the file 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Config\machine.config'. Make sure that the 'enterpriseLibrary.configurationSections' configuration section exists in one of the files and that the section 'dataConfiguration' is defined.
---------------------------

4.3如果WinFormApp1.exe.config存在,并且dataConfiguration配置正确,
那么db = be.GetDatabase();会成功。

在以上三种情况下,BookingUIP.dll.config和dataconfiguration.config始终存在,并且BookingUIP.dll.config正确连接到dataconfiguration.config.

我的问题是,如何能让 WinFormApp1.exe调用BookingUIP.dll的方法,同时BookingUIP.dll内面的Enterprise Library代码(包括Tracer)都引用BookingUIP.dll.config作为自己的配置文件,而不是WinAppForm1.exew.config?

转载于:https://www.cnblogs.com/zhaowd2001/archive/2005/10/24/260848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值