2013-04-29 60 views
3

我有一个自定义的截面的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");我得到以下异常:

出错创建CON用于注册的配置节处理程序:给定的程序集名称或代码库无效。 (例外从HRESULT:0x80131047)(C:\ ... \ web.config行13)

我也使用Unity,但不知道是否以任何方式与错误相关。

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

回答

5

您在App.Config中的部分的type属性中缺少命名空间。事实上,你不需要那里的全部程序集信息。只有命名空间是足够

更新1

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

在配置文件中只写

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

我刚刚注意到了!废话...修正它,现在我有'System.Configuration.IgnoreSectionHandler,系统,版本= 2.0.0.0,文化=中立,PublicKeyToken = b77a5c561934e089'。我没有收到任何错误,但该部分以“null”形式返回。任何想法如何获得部分内容? – JohnDoDo 2013-04-29 18:19:51

+0

更新了答案 – 2013-04-29 18:26:15

+0

终于明白null是从哪里来的。我一直愚蠢的一次通过混乱的类型值,他们愚蠢的第二次不明白为什么我得到一个null。那么,这是一个IgnoreSectionHandler,它的Create方法返回null。卫生署! – JohnDoDo 2013-04-30 07:49:26

7

鉴于这种的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; } 
     } 

    } 
} 
+4

我在部分名称(

)声明中缺少命名空间“MyApp”。干杯! – bizl 2015-02-27 01:17:46

+0

我改变了我的名字空间例如将MyApp.Configuration添加到Myapp.Configuration - 记住程序集名称区分大小写。 (感谢@bizl你的评论给了我想法检查它) – Aligma 2015-10-18 03:18:38