2013-04-29 64 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