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

发布于 2024-07-12 03:52:04 字数 3205 浏览 7 评论 0原文

我被困住了! 这看起来真的很愚蠢,但我看不出我哪里出了问题。 我正在创建一个 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);
                }
            }
        }
    }
}

Web 配置文件有此部分:

<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>

I am stuck! this seems really daft but I can not see where I am going wrong. I am creating a 2.0 C# ASP.NET website. I am trying to use a custom section in the web.config file with:

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

I have a separate DLL for the Objects which are in Bailey.DataLayer namespace. But when I run the test.aspx page I get the following error:

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);
                }
            }
        }
    }
}

The web config file has this section:

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

I have done this in a console app without a problem but can not see any differences apart from it being in a web page.

EDIT

It all compiles and throws the error at runtime so I can only assume it find the assembly because it is referenced in the test.aspx.cs page.

I have the following using statement at the top of the test.aspx.cs page:

using Bailey.DataLayer;

Here is the whole web.config file so there is no confusion:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭了丶谁疼 2024-07-19 03:52:04

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

(请参阅我对该问题的评论以进行澄清)。

Either you're using the wrong name (i.e. it's not called Bailey.DataLayer.dll), or it's not being copied to the bin directory on build. This last one doesn't seem likely however.

(See my comments on the question for clarification).

孤云独去闲 2024-07-19 03:52:04

好吧...我也有同样的问题。 上述解决方案都没有帮助。
就我而言,我的配置文件与 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 后,它就开始工作了。

Ok ... I had the same issue. None of the above solutions helped.
In my case my config file was in the same dll as that of web.config. I simply removed the namespace from the config section and that fixed my issue.

Not working

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

Working

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

As soon as I removed the namespace , ProjectName.ClientApi.Filters it started working.

淡忘如思 2024-07-19 03:52:04

您需要在配置文件中包含两个条目,一个在 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>

You need two entries in the config file, one on the configSections element to declare the custom config section, and another - the actual custom config section itself. Did you add both?

for example:

<?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>
御守 2024-07-19 03:52:04

我知道这已经非常旧了,但我无法看到同一问题的任何解决方案。 我有一段时间有下面的内容,当我从使用此配置文件的 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"/>

I know this is super old but I hadn't been able to see any solutions for the same issue. I had the below for a while, and it was working when I referenced the Common.Support.dll from the the exe that used this config file. To load the config file without having it associated to an exe, I had to do the below.

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

To

<section name="PasswordData" type="Libs.Common.Support.PasswordSection,Common.Support, Version=3.2.0.4, Culture=neutral, PublicKeyToken=null"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文