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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/452004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:55:16  来源:igfitidea点击:

C# ConfigurationManager.GetSection could not load file or assembly

c#configurationmanagerconfigsection

提问by Jon

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:

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

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:

我有一个单独的 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"

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:

Web 配置文件包含以下部分:

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

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

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

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

using Bailey.DataLayer;

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

这是整个 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>

采纳答案by Andrew Rollings

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.

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

(See my comments on the question for clarification).

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

回答by Charles Bretana

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?

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

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>

回答by Andrew Rollings

You may want to have a look at http://www.primaryobjects.com/CMS/Article81.aspxwhich contains, almost line for line, the code that you have shown above, including an example from a web.config file.

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

回答by Rwiti

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.

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

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.Filtersit started working.

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