2011-05-21 54 views
0

我創建了一個自定義類,從System.Configuration.ConfigurationSection繼承,像這樣實現的(這顯然只是爲了獲得真正的配置運行):ASP.NET自定義配置節從引用的項目不工作?

public class Config : ConfigurationSection 
{ 
    [ConfigurationProperty("quoteOfTheDay", DefaultValue = "It is what it is.", IsRequired = false)] 
    public string QuoteOfTheDay 
    { 
     get 
     { 
      return this["quoteOfTheDay"] as string; 
     } 
    } 

    [ConfigurationProperty("yourAge", IsRequired = true)] 
    public int YourAge 
    { 
     get 
     { 
      return (int)this["yourAge"]; 
     } 
    } 
} 

類的完整的命名空間+名是MyApp.Core。配置,它位於在編譯的MyApp.Core.dll組裝MyApp.Core項目。我然後從我的ASP.NET/MVC3項目,該項目被稱爲MyApp.UI引用這個項目。我已經編輯了Web.config文件,添加sectionGroupsection內容是這樣的:

<configSections> 
    <sectionGroup name="myAppConfigGroup"> 
     <section name="myAppSection" 
      type="MyApp.Core.Config, MyApp.Core, Version=1.0.0.0, Culture=neutral, PublicKey=null" 
      allowLocation="true" 
      allowDefinition="Everywhere" /> 
    </sectionGroup> 
    </configSections> 

    <myAppConfigGroup> 
    <myAppSection> 

    </myAppSection> 
    </myAppConfigGroup> 

但是,它似乎並不喜歡的類型MyApp.Core.Config習慣驗證配置或提供智能影音意義,因爲我編輯的組態。這裏的一些看法我做:

  • 我所定義的yourAge財產IsRequired = true和應用程序運行正常,沒有它。
  • 我甚至可以改變類型(有些類型根本不存在像Foo.Bar)在<section>元素,一切仍然運行良好
  • 但如果我刪除<section>元素,應用程序不跑。

我百思不得其解,任何幫助嗎?

回答

1

你可能不使用配置? 如果你不使用它,它只會做,如果根節點通過configSections註冊的檢查,但它不會看在這一點上的細節。

當你實際使用ConfigurationManager.GetSection的設置將被驗證,並在此配置的情況下加載它,它會觸發異常告知所需YourAge屬性缺失。

相關問題