posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

        前几天在做一个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

posted on   PetterLiu  阅读(2914)  评论(2编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示