C# 创建配置节处理程序时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16285169/
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-10 00:38:40  来源:igfitidea点击:

An error occurred creating the configuration section handler

c#asp.netc#-4.0web-configunity-container

提问by JohnDoDo

I have a dot.NET 4.0 web application with a custom section defined:

我有一个 dot.NET 4.0 Web 应用程序,其中定义了一个自定义部分:

<configuration>
    <configSections>
    <section name="registrations" type="System.Configuration.IgnoreSectionHandler, System.Configuration, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>
    ....

at the end of the web.config file I have the respective section:

在 web.config 文件的末尾,我有相应的部分:

  ....
  <registrations>
    .....
  </registrations>
</configuration>

Every time I call System.Configuration.ConfigurationManager.GetSection("registrations");I get the following exception:

每次我打电话时,System.Configuration.ConfigurationManager.GetSection("registrations");我都会收到以下异常:

An error occurred creating the configuration section handler for registrations: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) (C:\...\web.config line 13)

为注册创建配置节处理程序时发生错误:给定的程序集名称或代码库无效。(来自 HRESULT 的异常:0x80131047)(C:\...\web.config 第 13 行)

I'm also using Unity but don't know if that's in any way related to the error.

我也在使用 Unity,但不知道这是否与错误有关。

Have you faced this error before? How can I fix it? Do I need to replace the IgnoreSectionHandlerwith something else?

你以前遇到过这个错误吗?我该如何解决?我需要IgnoreSectionHandler用其他东西代替吗?

采纳答案by Shafqat Masood

You are missing the Namespace in the type attribute of your section in App.Config. Infact you dont need the full assembly info in there either. only the namespace is enough

您在 App.Config 部分的 type 属性中缺少命名空间。事实上,您也不需要那里的完整装配信息。只有命名空间就足够了

Updated 1

更新 1

  yourcustomconfigclass config =(yourcustomconfigclass)System.Configuration.ConfigurationManager.GetSection(
    "registrations");

and in config file only write

并在配置文件中只写

   <section name="registrations" type="System.Configuration.IgnoreSectionHandler" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>

回答by Doug Wilson

Given this app.config:

鉴于此 app.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="registrations" type="MyApp.MyConfigurationSection, MyApp" />
    </configSections>
    <registrations myValue="Hello World" />
</configuration>

Then try using this:

然后尝试使用这个:

namespace MyApp
{
    class Program
    {
        static void Main(string[] args) {
            var config = ConfigurationManager.GetSection(MyConfigurationSection.SectionName) as MyConfigurationSection ?? new MyConfigurationSection();

            Console.WriteLine(config.MyValue);

            Console.ReadLine();
        }
    }

    public class MyConfigurationSection : ConfigurationSection
    {
        public const String SectionName = "registrations";

        [ConfigurationProperty("myValue")]
        public String MyValue {
            get { return (String)this["myValue"]; }
            set { this["myValue"] = value; }
        }

    }
}