none
Custom section in app.config in windows service RRS feed

  • Question

  • Hello,

    I'm creating a windows service and I need to create custom configSections, but something is always wrong and I really don't know what. I have read a lots of topics on creating custom config sections, but nothing's goodddddddd

    My class definition

    namespace WebSync
    {
        public class ConfigHandler : ConfigurationSection
        {
            [ConfigurationProperty("mysql", IsRequired=true)]
            public MySQLElement Mysql
            {
                get { return (MySQLElement)this["mysql"]; }
                set { this["mysql"] = value; }
            }
        }
    
        public class MySQLElement : ConfigurationElement
        {
            [ConfigurationProperty("server", IsRequired = true)]
            public string Server
            {
                get { return (string)this["server"]; }
                set { this["server"] = value; }
            }
            [ConfigurationProperty("port", DefaultValue = 3306, IsRequired = false)]
            public int Port
            {
                get { return (int)this["port"]; }
                set { this["port"] = value; }
            }
            [ConfigurationProperty("user", IsRequired = true)]
            public string User
            {
                get { return (string)this["user"]; }
                set { this["user"] = value; }
            }
            [ConfigurationProperty("password", IsRequired = true)]
            public string Password
            {
                get { return (string)this["password"]; }
                set { this["password"] = value; }
            }
            [ConfigurationProperty("database", IsRequired = true)]
            public string Database
            {
                get { return (string)this["database"]; }
                set { this["database"] = value; }
            }
        }
    }

    and xml

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="dataConnections" type="WebSync.ConfigHandler,WebSync" allowLocation="true" allowDefinition="Everywhere"/>
      </configSections>
      <dataConnections>
        <mysql server="localhost" port="3306" database="c20165developdb" user="c20165develop" password="a1.b2.c3" />
      </dataConnections>
    </configuration>

    I really don't know what I did wrong, but I did it as it was shown on MSDN and lots of other pages.

    Sunday, June 10, 2012 2:20 PM

Answers

  • Hello, 

    What issue you are facing?

    I don't see any problem from above code. access config section like,

    ConfigHandler config = (ConfigHandler)System.Configuration.ConfigurationManager.GetSection("dataConnections");

     Hope this helps you...

    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by Vyrin Tuesday, June 12, 2012 4:03 PM
    Sunday, June 10, 2012 4:48 PM

All replies

  • Hello, 

    What issue you are facing?

    I don't see any problem from above code. access config section like,

    ConfigHandler config = (ConfigHandler)System.Configuration.ConfigurationManager.GetSection("dataConnections");

     Hope this helps you...

    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by Vyrin Tuesday, June 12, 2012 4:03 PM
    Sunday, June 10, 2012 4:48 PM
  • The problem is that it doesn't find it. The method returns null and in xml I don't have dataConnections and mysql tags in intellisense
    Sunday, June 10, 2012 6:01 PM