locked
Configuration system failed to initialize RRS feed

  • Question

  • Hi,

    I am using .NET 4.5 with VS2012. I have a solution with windows service project and a class library project (FileMove). Both are written in C#.

    My app.config in windows service has following configuration

    <ThreadCollection>		<thread assembly="FileMove" startHour="1" startMinute="0" stopHour="23" stopMinute="0" runInterval="1" trace="" />	</ThreadCollection>

    and my windows service uses following code to read the configuration:

    _threadConfigurations = (ConfigurationServiceSection)ConfigurationManager.GetSection("ThreadCollection")

    Idea is to call FileMove application as thread from windows service. 

    Then I installed the windows service using installutil.exe. Service got installed and started successfully but throwing following error when its execute the above code.

    Error 'Configuration system failed to initialize' occured  in System.Configuration   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)



    Tuesday, August 20, 2013 7:42 PM

Answers

  • Your config section is not configured properly.  You will have only a single entry in the <configSections> and it will reference your root element (infiniteTaskScheduler).  You will use your ConfigurationSectionService type to handle that section.  When the subsystem sees your custom element it will load the corresponding type and then pass the child XML to it for parsing.  Your CSS type needs to handle any child elements.  In your case you want to have a list of threads.  Without trying the code it looks like your other 2 types should be properly set up to handle the collection.  However collection elements don't quite work the way you'd expect.  Based upon your CSS type the subsystem is expecting a single element called thread that is a collection of child elements.  The default behavior is to have each child element (ConfigurationService instance) added to the collection using the add element like so:

    <infiniteTaskScheduler>
       <thread>
          <add assembly="..." />
          ...

    If you want to have the thread collection be the default collection then you need to configure it as the default collection.  You'll also need to change the element name for adding new items to "thread" based upon your XML you gave.  I wrote an article years ago on custom config sections.  At the bottom of that article I discuss how to get the code configured to have it set up the way you want.  Refer to the article for help getting the collection stuff set up properly.  It is, unfortunately, not straightforward or well documented.

    • Marked as answer by DOTNETDEV1983 Friday, August 30, 2013 1:53 PM
    Tuesday, August 27, 2013 3:00 AM

All replies

  • Hello DOTNETDEV1983,

    Welcome to the MSDN forum.

    I'll move your case to C# which may more appropriate for this issue.

    Regards,


    Barry Wang
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 22, 2013 5:00 AM
  • Can someone please help me with this issue?
    Friday, August 23, 2013 3:28 PM
  • You will get this error if the config file is bad.  Your <ThreadCollection> element is a custom element and therefore you must also have an entry in the <configSections> that defines your custom element and what type (and assembly) implements the custom section.  Refer to MSDN on how to create custom configuration elements.  Note that the article mentions ASP.NET but it applies to any application type.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Proposed as answer by KaranITI4DMV Sunday, May 25, 2014 11:49 PM
    • Unproposed as answer by KaranITI4DMV Sunday, May 25, 2014 11:49 PM
    Friday, August 23, 2013 5:07 PM
  • Hi CoolDadTx,

    Thanks for your reply

    I am still getting following error while calling : (Please see my app.config and custom config handler in the end)

    _threadConfigurations = (ConfigurationServiceSection)ConfigurationManager.GetSection("infiniteTaskScheduler");

    Error:

    'Unrecognized attribute 'assembly'. Note that attribute names are case-sensitive. (C:\ITS\InfiniteTaskScheduler\InfiniteTaskScheduler\bin\Debug\InfiniteTaskScheduler.exe.Config line 29)' occured  in System.Configuration   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, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, 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 InfiniteTaskScheduler.InfiniteTaskScheduler.LoadConfiguration() in c:\ITS\InfiniteTaskScheduler\InfiniteTaskScheduler\InfiniteTaskScheduler.cs:line 114
       at InfiniteTaskScheduler.InfiniteTaskScheduler.OnStart(String[] args) in c:\ITS\InfiniteTaskScheduler\InfiniteTaskScheduler\InfiniteTaskScheduler.cs:line 53


    App.Config has

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<configSections>
    		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    		<section name="infiniteTaskScheduler" type="InfiniteTaskScheduler.ConfigurationServiceSection, InfiniteTaskScheduler"/>
    		<section name="thread" type="InfiniteTaskScheduler.ConfigSectionHandler, InfiniteTaskScheduler"/>
    		
    	</configSections>
    	<log4net>
    		<appender name="mainTrace" type="log4net.Appender.RollingFileAppender">
    			<file type="log4net.Util.PatternString" value="mylogfile_%date{yyyyMMdd}.log" />
    			<file type="log4net.Util.PatternString" value="my.log" />
    			<appendToFile value="true"/>
    			<rollingStyle value="Date"/>
    			<datePattern value="yyyyMMdd" />
    			<layout type="log4net.Layout.PatternLayout">
    				<conversionPattern value="[%date] [%-5level] [%30type] [%30method] [%4line] [%50username] [%message]%newline"/>
    			</layout>
    		</appender>
    		<root>
    			<level value="INFO"/>
    			<appender-ref ref="mainTrace"/>
    		</root>
    	</log4net>
    	<infiniteTaskScheduler>
    		<!--<thread assembly="Jobs" startHour="1" startMinute="0" stopHour="23" stopMinute="0" runInterval="1" trace="" />
    		<thread assembly="Validator" startHour="1" startMinute="0" stopHour="23" stopMinute="0" runInterval="1" trace="" />
    		<thread assembly="DataPump" startHour="1" startMinute="0" stopHour="23" stopMinute="0" runInterval="1" trace="" />-->
    		<thread assembly="MoveFile" startHour="1" startMinute="0" stopHour="23" stopMinute="0" runInterval="1" trace="" />
    	</infiniteTaskScheduler>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
    </configuration>


    And CustomConfigHandler has

    using System;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace InfiniteTaskScheduler
    {
        public sealed class ConfigurationServiceSection : ConfigurationSection
        {
            private static readonly ConfigurationProperty threadsElement = new ConfigurationProperty("thread", typeof(ConfigurationServices), null, ConfigurationPropertyOptions.IsRequired);
    
            public ConfigurationServiceSection()
            {
                base.Properties.Add(threadsElement);
            }
    
            [ConfigurationProperty("thread", IsRequired = true)]
            public ConfigurationServices ConfigurationServices
            {
                get
                {
                    return (ConfigurationServices)this[threadsElement];
                }
            }
        }
    
        [ConfigurationCollection(typeof(ConfigurationService), AddItemName = "thread")]
    
        public sealed class ConfigurationServices : ConfigurationElementCollection
        {
            
            protected override ConfigurationElement CreateNewElement()
            {
                return new ConfigurationService();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ConfigurationService)element).Assembly;
            }
    
            new public ConfigurationService this[string name]
            {
                get { return (ConfigurationService)BaseGet(name); }
            }
        }
    
        public sealed class ConfigurationService : ConfigurationElement
        {
            [ConfigurationProperty("assembly", IsKey = true, IsRequired = true)]
            public string Assembly
            {
                get { return (string)this["assembly"]; }
                set { this["assembly"] = value; }
            }
    
            [ConfigurationProperty("class", IsKey = true, IsRequired = true)]
            public string Class
            {
                get { return (string)this["class"]; }
                set { this["class"] = value; }
            }
    
            [ConfigurationProperty("startHour", IsRequired = true)]
            [IntegerValidator(MinValue = 0, MaxValue = 23)]
            public int StartHour
            {
                get { return (int)this["startHour"]; }
                set { this["startHour"] = value; }
            }
    
            [ConfigurationProperty("startMinute", IsRequired = true)]
            [IntegerValidator(MinValue = 0, MaxValue = 59)]
            public int StartMinute
            {
                get { return (int)this["startMinute"]; }
                set { this["startMinute"] = value; }
            }
    
            [ConfigurationProperty("stopHour", IsRequired = true)]
            [IntegerValidator(MinValue = 0, MaxValue = 23)]
            public int StopHour
            {
                get { return (int)this["stopHour"]; }
                set { this["stopHour"] = value; }
            }
    
            [ConfigurationProperty("stopMinute", IsRequired = true)]
            [IntegerValidator(MinValue = 0, MaxValue = 59)]
            public int StopMinute
            {
                get { return (int)this["stopMinute"]; }
                set { this["stopMinute"] = value; }
            }
    
            [ConfigurationProperty("runInterval", IsRequired = true)]
            [IntegerValidator(MinValue = 0, MaxValue = 5183999)]
            public int RunInterval
            {
                get { return (int)this["runInterval"]; }
                set { this["runInterval"] = value; }
            }
    
            [ConfigurationProperty("trace", IsRequired = false)]
            public string Trace
            {
                get { return (string)this["trace"] == string.Empty ? null : (string)this["trace"]; }
                set { this["trace"] = value; }
            }
        }
    }




    • Edited by DOTNETDEV1983 Tuesday, August 27, 2013 2:43 AM Duplicated error logs
    Tuesday, August 27, 2013 2:42 AM
  • Your config section is not configured properly.  You will have only a single entry in the <configSections> and it will reference your root element (infiniteTaskScheduler).  You will use your ConfigurationSectionService type to handle that section.  When the subsystem sees your custom element it will load the corresponding type and then pass the child XML to it for parsing.  Your CSS type needs to handle any child elements.  In your case you want to have a list of threads.  Without trying the code it looks like your other 2 types should be properly set up to handle the collection.  However collection elements don't quite work the way you'd expect.  Based upon your CSS type the subsystem is expecting a single element called thread that is a collection of child elements.  The default behavior is to have each child element (ConfigurationService instance) added to the collection using the add element like so:

    <infiniteTaskScheduler>
       <thread>
          <add assembly="..." />
          ...

    If you want to have the thread collection be the default collection then you need to configure it as the default collection.  You'll also need to change the element name for adding new items to "thread" based upon your XML you gave.  I wrote an article years ago on custom config sections.  At the bottom of that article I discuss how to get the code configured to have it set up the way you want.  Refer to the article for help getting the collection stuff set up properly.  It is, unfortunately, not straightforward or well documented.

    • Marked as answer by DOTNETDEV1983 Friday, August 30, 2013 1:53 PM
    Tuesday, August 27, 2013 3:00 AM
  • Thanks CoolDadTx!!! It worked!!!!
    Friday, August 30, 2013 1:52 PM