none
Adding own configSection to App.Config

    Вопрос

  • Hello.

    I tried to add section in App.Config.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="workers" type="MyProject.Setup.Config.WorkerElement, MyProject" />
      </configSections>
      <workers>
        <worker name="Name" type="Operator" />
      </workers>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
      </startup>
    </configuration>

    Classes:

    public class WorkersConfigSection : ConfigurationSection
        {
            [ConfigurationProperty("workers")]
            public WorkersCollection WorkersCollection
            {
                get => ((WorkersCollection) base["workers"]);
                set => base["workers"] = value;
            }
        }
    
    [ConfigurationCollection(typeof(WorkerElement), AddItemName = "worker")]
        public class WorkersCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new WorkerElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((WorkerElement)element).Name;
            }
        }
    
    
    public class WorkerElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
            public string Name
            {
                get => (string)base["name"];
                set => base["name"] = value;
            }
    
            [ConfigurationProperty("type", IsKey = false, IsRequired = true)]
            public WorkerType Type
            {
                get => (WorkerType)base["type"];
                set => base["type"] = value;
            }
        }

    But on debug I catch exception Type 'MyProject.Setup.Config.WorkerElement' does not implement 'System.Configuration.IConfigurationSectionHandler'.

    Where i made mistake?

    11 февраля 2019 г. 1:26

Все ответы

  • Hi  fadeinmad,  

    >>But on debug I catch exception Type 'MyProject.Setup.Config.WorkerElement' does not implement 'System.Configuration.IConfigurationSectionHandler'.

    Please refer the following code sample and modify your application.

     <configSections>
        <section name="WorkersConfigSection" type="WpfApp_temp2.WorkersConfigSection, WpfApp_temp2" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
      </startup>
      <WorkersConfigSection>
        <workers>
          <worker name="Name" type="Operator" />
          <worker name="Name2" type="Operator2"></worker>
        </workers>
      </WorkersConfigSection>
    
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var connectionManagerDataSection = ConfigurationManager.GetSection(WorkersConfigSection.SectionName) as WorkersConfigSection;
                if (connectionManagerDataSection != null)
                {
                    foreach (WorkerElement endpointElement in connectionManagerDataSection.WorkersCollection)
                    {
                        var endpoint = new WorkerElement() { Name = endpointElement.Name , Type=endpointElement.Type };
                        MessageBox.Show("Name: "+endpoint.Name+"; Type: "+ endpoint.Type);
                    }
                }
            }
    
    
        public  class WorkersConfigSection : ConfigurationSection
        {
    
            public const string SectionName = "WorkersConfigSection";
    
            [ConfigurationProperty("workers")]
            [ConfigurationCollection(typeof(WorkersCollection), AddItemName = "worker")]
            public WorkersCollection WorkersCollection
            {
                get => ((WorkersCollection)base["workers"]);
                set => base["workers"] = value;
            }
        }
    
    
        public class WorkersCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new WorkerElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((WorkerElement)element).Name;
            }
        }
    
    
    
        public class WorkerElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
            public string Name
            {
                get => (string)base["name"];
                set => base["name"] = value;
            }
    
            [ConfigurationProperty("type", IsKey = false, IsRequired = true)]
            public string Type
            {
                get => (string)base["type"];
                set => base["type"] = value;
            }
        }
    



    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread. 


    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    12 февраля 2019 г. 5:16
    Модератор