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

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

我有一个定义了自定义部分的 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"); 时都会出现以下异常:

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)

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

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

最佳答案

鉴于此 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; }
        }

    }
}

关于c# - 创建配置节处理程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285169/

相关文章:

c# - 如何将此递归函数转换为迭代函数?

asp.net - 建立与 SQL Server 的连接时出现与网络相关或特定于实例的错误

c# - 如何发送og :Title og:Image og:Description og:url info from C# to Facebook

asp.net-mvc-3 - 在 MVC3 中创建由其他模型的对象组成的 View 模型

c# - 从 Entity Framework 表中选择特定列

c# - SQL 在负载测试中拒绝连接

c# - 从 C# 运行 Powershell

javascript - asp 网络客户端验证的验证事件

c# - 区分 "Refresh Post"或 "Real Post Back"的最佳方法是什么

c# - 按名称访问动态对象属性