2012-02-14 64 views
7

我有困難閱讀使用ConfigurationManager的app.config。我有一個自定義部分,它使用ConfigurationElementCollection。閱讀自定義配置節返回「無效的鍵值」

我(切下)XML:

<configuration> 
    <configSections> 
     <sectionGroup name ="CustomerMatching"> 
      <section name="SearchWeight" type="BLL.Contracts.Helpers.CustomerMatching.SearchWeightSection, BLL.Contracts"/> 
     </sectionGroup> 
    </configSections> 

    <CustomerMatching> 
     <SearchWeight> 
      <methods> 
       <method SearchMethod="ByContactName" Weight="100"/> <!--Line 53, referenced in Error --> 
       <method SearchMethod="ByBusinessName" Weight="250"/> 
       <method SearchMethod="ByPostcode" Weight="250"/> 
       <method SearchMethod="ByMobile" Weight="500"/> 
       <method SearchMethod="ByLandline" Weight="500"/> 
       <method SearchMethod="ByCompCharNo" Weight="850"/> 
      </methods> 
     </SearchWeight> 
    </CustomerMatching> 
</configuration> 

我的配置類:

public class SearchWeightSection: ConfigurationSection 
{ 
    [ConfigurationProperty("methods", IsRequired = true)] 
    [ConfigurationCollection(typeof(SearchMethods), AddItemName = "method", CollectionType = ConfigurationElementCollectionType.BasicMap)] 
    public SearchMethods SearchMethods 
    { 
     get { return (SearchMethods) base["methods"]; } 
    } 
} 

public class SearchMethods: ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new Method(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     var method = (Method) element; 

     return method.SearchMethod; 
    } 

    public new Method this[string index] 
    { 
     get { return (Method)BaseGet(index); } 

    } 
} 

public class Method: ConfigurationElement 
{ 
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)] 
    public string SearchMethod { get; set; } 

    [ConfigurationProperty("Weight", IsRequired = true)] 
    public string Weight { get; set; } 

    public Method(string searchMethod, string weight) 
    { 
     SearchMethod = searchMethod; 
     Weight = weight; 
    } 

    public Method() 
    { 

    } 
} 

嘗試使用它:

[TestMethod] 
    public void TestConfigReader() 
    { 
     var searchSection = (SearchWeightSection)ConfigurationManager.GetSection("CustomerMatching/SearchWeight"); 
     Assert.AreEqual(searchSection.SearchMethods["ByContactName"].Weight, "100"); 
    } 

我的錯誤:

Test method BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader threw exception: 
System.Configuration.ConfigurationErrorsException: Invalid key value. (C:\Users\michael\Documents\Visual Studio 2010\MyProject\BLL.UnitTests\bin\Debug\BLL.UnitTests.dll.config line 53) 
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) 
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, ref Object result, ref Object resultRuntimeObject) 
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject) 
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject) 
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject) 
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) 
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) 
at System.Configuration.ConfigurationManager.GetSection(String sectionName) 
at BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader() in CustomerMatchManagerTest.cs: line 41 

謝謝。

+0

看看這個:https://stackoverflow.com/questions/43037691/design-to-implement-a-wrapper-for-configurationsection-net-class這是工作 – 2017-10-26 04:18:26

回答

13

你的代碼看起來精絕,除了方法配置元素本身。

因爲它是一個結構元件,屬性需要的數據存儲在所述基類(的ConfigurationElement)名稱值集合。否則,鍵和值不會被初始化。

public class Method: ConfigurationElement 
{ 
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)] 
    public string SearchMethod 
    { 
     get { return base["SearchMethod"] as string; } 
     set { base["SearchMethod"] = value; } 
    } 

    [ConfigurationProperty("Weight", IsRequired = true)] 
    public string Weight 
    { 
     get { return base["Weight"] as string; } 
     set { base["Weight"] = value; } 
    } 

    //REST OF YOUR CLASS 
} 

希望有所幫助。

+0

神奇的,這完美的工作。 – 2012-02-14 12:12:16