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 3231206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:01:04+00:00 2026-05-17T17:01:04+00:00

I am trying to implement a custom configuration section in a project and I

  • 0

I am trying to implement a custom configuration section in a project and I keep running up against exceptions that I do not understand. I am hoping someone can fill in the blanks here.

I have App.config that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/>
    </configSections>
    <ServicesSection type="RT.Core.Config.ServicesSection, RT.Core">
            <Services>
                <AddService Port="6996" ReportType="File" />
                <AddService Port="7001" ReportType="Other" />
            </Services>
        </ServicesSection>
</configuration>

I have a ServiceConfig element defined like so:

public class ServiceConfig : ConfigurationElement
  {
    public ServiceConfig() {}

    public ServiceConfig(int port, string reportType)
    {
      Port = port;
      ReportType = reportType;
    }

    [ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
    public int Port 
    {
      get { return (int) this["Port"]; }
      set { this["Port"] = value; }
    }

    [ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)]
    public string ReportType
    {
      get { return (string) this["ReportType"]; }
      set { this["ReportType"] = value; }
    }
  }

And I have a ServiceCollection defined like so:

public class ServiceCollection : ConfigurationElementCollection
  {
    public ServiceCollection()
    {
      Console.WriteLine("ServiceCollection Constructor");
    }

    public ServiceConfig this[int index]
    {
      get { return (ServiceConfig)BaseGet(index); }
      set
      {
        if (BaseGet(index) != null)
        {
          BaseRemoveAt(index);
        }
        BaseAdd(index, value);
      }
    }

    public void Add(ServiceConfig serviceConfig)
    {
      BaseAdd(serviceConfig);
    }

    public void Clear()
    {
      BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
      return new ServiceConfig();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((ServiceConfig) element).Port;
    }

    public void Remove(ServiceConfig serviceConfig)
    {
      BaseRemove(serviceConfig.Port);
    }

    public void RemoveAt(int index)
    {
      BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
      BaseRemove(name);
    }
  }

The part I am missing is what to do for the handler. Originally, I tried to implement an IConfigurationSectionHandler but found two things:

  1. it didn’t work
  2. it’s deprecated.

I’m completely lost now on what to do so I can read my data from config. Any help please!

  • 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-17T17:01:05+00:00Added an answer on May 17, 2026 at 5:01 pm

    The previous answer is correct but I’ll give you all the code as well.

    Your app.config should look like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <configSections>
          <section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core"/>
       </configSections>
       <ServicesSection>
          <Services>
             <add Port="6996" ReportType="File" />
             <add Port="7001" ReportType="Other" />
          </Services>
       </ServicesSection>
    </configuration>
    

    Your ServiceConfig and ServiceCollection classes remain unchanged.

    You need a new class:

    public class ServiceConfigurationSection : ConfigurationSection
    {
       [ConfigurationProperty("Services", IsDefaultCollection = false)]
       [ConfigurationCollection(typeof(ServiceCollection),
           AddItemName = "add",
           ClearItemsName = "clear",
           RemoveItemName = "remove")]
       public ServiceCollection Services
       {
          get
          {
             return (ServiceCollection)base["Services"];
          }
       }
    }
    

    And that should do the trick. To consume it you can use:

    ServiceConfigurationSection serviceConfigSection =
       ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;
    
    ServiceConfig serviceConfig = serviceConfigSection.Services[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a custom UIView that mimics the behavior of UIScrollView for
I'm trying to implement a custom SessionIDManager very similar this example . I'm putting
i am trying to implement dependency injection for our custom ModelBinders. Right now i
I am trying to implement a custom picker. It has 3 components. Now, I
I am trying to implement a custom menu for Internet Explorer 7.0. For this
I am trying to implement a custom titlebar: Here is my Helper class: import
I am trying to implement a custom Java widget using GWT.This requires me to
Possible Duplicate: Calling a webservice that uses ISO-8859-1 encoding from WCF I am trying
This is my service configuration in web.config: <binding name=statefulSessionWithUsernameOverTransport> <security authenticationMode=SecureConversation requireSecurityContextCancellation=False allowInsecureTransport=True> <secureConversationBootstrap

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.