C# ConfigurationManager.GetSection 无法加载文件或程序集。

16

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

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

我有一个专门为Bailey.DataLayer命名空间中的对象创建的独立DLL。但是当我运行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"
我尝试获取的类如下所示:

The class that I am trying to get is as follows:

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>

实际的自定义配置部分在哪里? - Andrew Rollings
好的,这就是问题所在……你实际上没有自定义部分。 - Andrew Rollings
愚蠢的问题:自定义dll文件名是什么? - Andrew Rollings
1
我在创建断点时就遇到了问题,按F11进入后,出现了ConfigurationErrorsException未处理的异常,提示无法加载Bailey.DataLayer!真是气死我了.. 感谢你的帮助Andrew。 - Jon
BaileyObjects.dll ............ 抱歉,我得遛狗了,所以回复有点晚。 - Jon
显示剩余2条评论
4个回答

27

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

(请参见我在问题上的评论以获得澄清)。


6
谢谢您,Andrew!我恍然大悟。在Web配置文件中没有提到DLL名称。我没有意识到实际上我需要放置的是DLL,我以为只需要命名空间即可。 - Jon
在http://msdn.microsoft.com/en-en/library/2tw134k3.aspx中描述了创建配置部分的过程。有一件事情是,具有该部分的程序集应与您要使用它的webconfig位于同一级别,如下所示: 将定义部分的程序集包含在与Web.config文件相同的目录中。 - Vladimir Shmidt

9

好的...我遇到过同样的问题。以上解决方案都没有帮助。 在我的情况下,我的配置文件与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,它就开始工作了。


4
您需要在配置文件中添加两个条目,一个是在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>

嗨,查尔斯,我已经编辑了我的帖子,试图显示整个配置文件(目前未正确显示)。但我相信它是正确的。我不知道DictionarySectionHandler,这很酷,我将来会使用它。谢谢。 - Jon
谢谢你提供关于System.Configuration.DictionarySectionHandler的提示,让我的工作变得轻松了很多! - paulH

0

我知道这个问题很老了,但我一直没有看到任何解决方案。我有以下代码已经运行了一段时间,当我从使用此配置文件的exe中引用Common.Support.dll时,它可以正常工作。为了在没有将其关联到exe的情况下加载配置文件,我必须执行以下操作。

<section name="PasswordData" type="Libs.Common.Support.PasswordSection,Common.Support"/>

<section name="PasswordData" type="Libs.Common.Support.PasswordSection,Common.Support, Version=3.2.0.4, Culture=neutral, PublicKeyToken=null"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接