【问题标题】:C# ConfigurationManager.GetSection could not load file or assemblyC# ConfigurationManager.GetSection 无法加载文件或程序集
【发布时间】:2010-10-01 21:47:08
【问题描述】:

我卡住了!这看起来真的很愚蠢,但我看不出我哪里出错了。我正在创建一个 2.0 C# ASP.NET 网站。我正在尝试使用 web.config 文件中的自定义部分:

DatabaseFactorySectionHandler sectionHandler = ConfigurationManager.GetSection("DatabaseFactoryConfiguration") as DatabaseFactorySectionHandler;

我有一个单独的 DLL 用于 Bailey.DataLayer 命名空间中的对象。但是当我运行 test.aspx 页面时,出现以下错误:

System.Configuration.ConfigurationErrorsException was unhandled by user code

Message="An error occurred creating the configuration section handler for DatabaseFactoryConfiguration: Could not load file or assembly 'Bailey.DataLayer' or one of its dependencies. The system cannot find the file specified. (C:\\Documents and Settings\\Administrator.PIP\\My Documents\\Visual Studio 2005\\WebSites\\bailey\\web.config line 13)"
Source="System.Configuration"

我想要的课程如下:

namespace Bailey.DataLayer
{
    public sealed class DatabaseFactorySectionHandler : ConfigurationSection
    {
        [ConfigurationProperty("Name")]
        public string Name
        {
            get { return (string)base["Name"]; }
        }

        [ConfigurationProperty("ConnectionStringName")]
        public string ConnectionStringName
        {
            get { return (string)base["ConnectionStringName"]; }
        }

        public string ConnectionString
        {
            get
            {
                try
                {
                    return ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
                }
                catch (Exception excep)
                {
                    throw new Exception("Connection string " + ConnectionStringName +
                                        " was not found in web.config. " + 
                                        excep.Message);
                }
            }
        }
    }
}

网络配置文件有这个部分:

<configSections>
  <section name="DatabaseFactoryConfiguration" 
           type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />
</configSections>

我在控制台应用程序中完成此操作没有问题,但除了在网页中之外看不出任何差异。

编辑

它全部编译并在运行时抛出错误,所以我只能假设它找到了程序集,因为它在 test.aspx.cs 页面中被引用。

我在 test.aspx.cs 页面顶部有以下 using 语句:

using Bailey.DataLayer;

这是整个 web.config 文件,所以没有混淆:

<configuration>
   <configSections>
      <section name="DatabaseFactoryConfiguration" type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />
   </configSections>
    <appSettings/>
   <connectionStrings>
      <add name="BaileyMDFConString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bailey.mdf;Integrated Security=True;User Instance=True"  providerName="System.Data.SqlClient" />
    </connectionStrings>
     <DatabaseFactoryConfiguration Name="System.Data.SqlClient" ConnectionStringName="BaileyMDFConString" />
   <system.web>         
      <compilation debug="true"/>       
      <authentication mode="Windows"/>  
   </system.web>
</configuration>

【问题讨论】:

  • 实际的自定义配置部分在哪里?
  • 那是你的问题......你实际上没有自定义部分。
  • 它在那里,但我无法从配置文件中获取 XML 以正确显示,或者现在根本无法显示!
  • 自定义配置让人头疼,尤其是当它们不太正确时...我的建议是在任何地方添加断点!
  • 我在创建它的时候有中断,我按 F11 进入它,我得到 ConfigurationErrorsException 未处理,说 Bailey.DataLayer 无法加载! GRRRRR.. 谢谢你的帮助安德鲁

标签: c# configurationmanager configsection


【解决方案1】:

要么您使用了错误的名称(即它不叫 Bailey.DataLayer.dll),要么它没有在构建时被复制到 bin 目录中。然而,最后一个似乎不太可能。

(请参阅我的 cmets 以获得澄清)。

【讨论】:

  • 谢谢安德鲁!它点击了。 Web 配置中未引用 DLL 名称。我没有意识到这是我必须放置的实际 DLL,我以为它只是名称空间。
  • 这里msdn.microsoft.com/en-en/library/2tw134k3.aspx 描述了创建配置部分的过程。还有一件事,带有节的程序集应该与您想使用它的 webconfig 处于同一级别,如下所示:将定义节的程序集包含在与 Web.config 文件相同的目录中。
【解决方案2】:

好的...我遇到了同样的问题。以上解决方案都没有帮助。 在我的情况下,我的配置文件与 web.config 在同一个 dll 中。我只是从配置部分中删除了命名空间,这解决了我的问题。

不工作

<configSections>
<section name="authorizedServerSection" type="ProjectName.ClientApi.Filters.AuthorizedServerSection, ProjectName.ClientApi.Filters" requirePermission="false"/>

工作

<configSections>
<section name="authorizedServerSection" type="ProjectName.ClientApi.Filters.AuthorizedServerSection" requirePermission="false"/>

我一删除命名空间, ProjectName.ClientApi.Filters,它就开始工作了。

【讨论】:

    【解决方案3】:

    您需要配置文件中的两个条目,一个在 configSections 元素上声明自定义配置部分,另一个 - 实际的自定义配置部分本身。两个都加了吗?

    例如:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <configSections>
        **<section name="Connections"
                 type="System.Configuration.DictionarySectionHandler" />**
      </configSections>
    
      <Connections 
            <add key="myServer" value="serverName" />
            <add key="myPort"   value="8080" />
            <add key="myURI"    value="RequestUri" />
            <add key="UserId"   value="joebob" />
            <add key="password" value="$^%^&%$^&@%" />        
       />
    
    </configuration>
    

    【讨论】:

    • 嗨查尔斯,我已经编辑了我的帖子以尝试显示整个配置文件(无法正确显示 atm。但我相信它是正确的。我不知道 DictionarySectionHandler 这很酷等等我以后会用。thx
    • +1 获取有关 System.Configuration.DictionarySectionHandler 的提示 - 让事情变得如此简单!
    【解决方案4】:

    您可能想查看http://www.primaryobjects.com/CMS/Article81.aspx,其中几乎逐行包含您上面显示的代码,包括来自 web.config 文件的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      相关资源
      最近更新 更多