Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:01:13+00:00 2026-05-30T00:01:13+00:00

I’m having difficulties reading an app.config using the ConfigurationManager. I have a custom section,

  • 0

I’m having difficulties reading an app.config using the ConfigurationManager. I have a custom section, which uses a ConfigurationElementCollection.

My (cut-down) 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>

My Configuration classes:

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()
    {

    }
}

Trying to use it:

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

My error:

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

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:01:15+00:00Added an answer on May 30, 2026 at 12:01 am

    Your code looks absolutely fine, apart from the Method configuration element itself.

    Since it’s a configuration element, properties need to store the data in the base class’ (ConfigurationElement) name value collection. Otherwise key and value will not be initialized.

    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
    }
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.