【问题标题】:An error occurred creating the configuration section handler创建配置节处理程序时出错
【发布时间】:2013-04-23 12:06:10
【问题描述】:

我有一个定义了自定义部分的 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"/>
    ....

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

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

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

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

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

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

【问题讨论】:

    标签: c# asp.net c#-4.0 web-config unity-container


    【解决方案1】:

    鉴于此 app.config:

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

    然后尝试使用这个:

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

    【讨论】:

    • 我在部分名称 (
      ) 声明中缺少命名空间“,MyApp”。干杯!
    • 我改变了我的名字空间的大小写,例如MyApp.Configuration 到 Myapp.Configuration - 请记住程序集名称区分大小写。 (感谢@bizl 你的评论给了我检查它的想法)
    • 我需要这个超级简单的样本来克服大脑锁定问题。添加命名空间就可以了,加上这个例子提供了足够的洞察力,可以看到我的其余代码是好的。谢谢!
    【解决方案2】:

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

    更新 1

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

    并且只在配置文件中写入

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

    【讨论】:

    • 我刚刚注意到了!废话...修复了它,现在我有System.Configuration.IgnoreSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089。我没有收到任何错误,但该部分返回为null。知道如何获取部分内容吗?
    • 终于弄清楚了 null 的来源。我曾经因为弄乱类型值而变得愚蠢,而他们因为不理解为什么我得到空值而第二次变得愚蠢。好吧,它是一个 IgnoreSectionHandler,它的 Create 方法返回 null。哇!
    猜你喜欢
    • 2013-04-28
    • 2013-05-08
    • 2015-11-06
    • 2012-05-11
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多