前几天在做一个Web Project升级过程中遇到这个问题,项目使用了Enterprise Library 4.1,并且有独立的配置文件。升级到5.0版本时发现找不到这个配置文件。
运行时将得到这样的Exception(这里我们定义配置文件为EntLib.config):
The configuration file EntLib.config could not be found.
解决方法有以下两种:
一 引用Microsoft.Practices.EnterpriseLibrary.Common.Configuration.dll, 自定义Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource,重写Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSourceElement下的CreateSource方法:
[Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationElementType(typeof(FileConfigurationSourceElement))]
public class FileConfigurationSource : Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource
{
public FileConfigurationSource(string configurationFilepath)
: base(configurationFilepath)
{
}
}
public class FileConfigurationSourceElement : Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSourceElement
{
/// <summary>
/// Returns a new <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource"/> configured with the receiver's settings.
/// </summary>
/// <returns>A new configuration source.</returns>
public override Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource CreateSource()
{
string configurationFilepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.FilePath);
return new FileConfigurationSource(configurationFilepath);
}
}
在Web.config/App.config中使用:
1: <configuration>
2: <configSections>
3: <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
4: </configSections>
5: <enterpriseLibrary.ConfigurationSource selectedSource="File-based Configuration Source"
6: parentSource="">
7: <sources>
8: <!--Customer-->
9: <add name="File-based Configuration Source" type="EntLibWebApplication.FileConfigurationSource,EntLibWebApplication"
10: filePath="EntLib.config" />
11: <add name="File-based Configuration Source2" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
12: filePath="EntLib.config" />
13: </sources>
14: </enterpriseLibrary.ConfigurationSource>
15: <system.web>
16: <compilation debug="true" targetFramework="4.0" />
17: </system.web>
18:
19: </configuration>
注意9,10行的关键配置节,使用了我们定义的FileConfigurationSource。下面那个是默认的,实际上可以不要了。
方法二,找开Commn Library 的源代码,找到FileConfigurationSource.cs文件,修改下面这个方法为:
private static string GetRootedCurrentConfigurationFile(string configurationFile)
{
if (string.IsNullOrEmpty(configurationFile))
throw new ArgumentException(Resources.ExceptionStringNullOrEmpty, "configurationFile");
string strPath =
Path.IsPathRooted(configurationFile)
? configurationFile
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configurationFile);
if (!File.Exists(strPath))
{
throw new FileNotFoundException(
string.Format(
CultureInfo.CurrentCulture,
Resources.ExceptionConfigurationLoadFileNotFound,
configurationFile));
}
return strPath;
}
这个方法不需要修改配置节。
希望对您的开发有帮肋。
作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog。