Configuration System Failed to Initialize
Make sure that your config file (web.config if web, or app.config if windows) in your project starts as:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="YourProjectName.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
</configuration>
Note that inside the configuration
element, the first child must be the configSections
element.
In the name
property on section
element, make sure you replace YourProjectName
with your actual project's name.
It happened to me that I created a webservice in a class library project, then I copied (overwriting) the config file (in order to bring the endpoints configuration) to my windows app and I started to have the same problem. I had inadvertently removed configSections
.
app.config - configuration system failed to initialize with custom section
I notice 2 things
A configuration section can only appear 1 time within a configuration file.
The full initialization exception shows:
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize --->
System.Configuration.ConfigurationErrorsException: Sections must only appear once per config file.
This means you can only have 1 product
section:
<productGroup>
<product name="my name" id="1" />
</productGroup>
If you need multiple, you need to declare multiple sections with unique section names:
<configuration>
<sectionGroup name="productGroup">
<section name="product" type="myProject.Stuff.Api.ProductSection" />
<section name="product2" type="myProject.Stuff.Api.ProductSection" />
</sectionGroup>
<!-- Other configuration elements -->
<productGroup>
<product name="my name" id="1" />
<product2 name="my name 2" id="2" />
</productGroup>
</configuration>
It is a best practice to declare the section in app/web.config
with its fully qualified assemblyname,
which includes the name of the assembly (without file extension).
This is a must if the class/section is defined in an external assembly; here ProductionSection
is defined in an assembly other than the one of the main unittest.
Declare the section as below in the App.config
file of your unittest project.
(Replace NameOfTheAssemblyContainingTheProductSectionClass with the name of your assembly.)
<section
name="product"
type="myProject.Stuff.Api.ProductSection, NameOfTheAssemblyContainingTheProductSectionClass"
/>
Configuration System Failed To Initialize
Try putting the configSections
as the first child element of configuration
, because configSections
should be the first element of configurations
So your config file will go like this:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Vegi_Manager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<connectionStrings>
<add name="ConStr" connectionString="Integrated Security=false;Persist Security Info=False;User ID=funny;password=veryfunny;Initial Catalog=vegimanager;Data Source=.\sqlexpress;"/>
</connectionStrings>
<userSettings>
<Vegi_Manager.Properties.Settings>
<setting name="FIRMNAME" serializeAs="String">
<value/>
</setting>
<setting name="FIRMADDRESS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMCITY" serializeAs="String">
<value/>
</setting>
<setting name="FIRMSTATE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPHONE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMOBILE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMEMAIL" serializeAs="String">
<value/>
</setting>
<setting name="FIRMTIN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPAN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMANDITAXNO" serializeAs="String">
<value/>
</setting>
<setting name="INITIALFONFIGDONE" serializeAs="String">
<value>False</value>
</setting>
<setting name="FIRMJURISDICTION" serializeAs="String">
<value/>
</setting>
<setting name="FIRMBANKDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="BILLFORMATNO" serializeAs="String">
<value>0</value>
</setting>
<setting name="PRINTERNAME" serializeAs="String">
<value/>
</setting>
</Vegi_Manager.Properties.Settings>
</userSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
How to solve App config error Configuration system failed to initialize
It seems like you're missing the appSettings
element that wraps your key-value collection. It should look more like the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DataSource" value=" My Database'IP adress "/>
<add key="Database" value="My database Name"/>
<add key="Name" value="User name"/>
<add key="Password" value="Password"/>
</appSettings>
</configuration>
Configuration system failed to initialize after adding a new external config file containing a new section
After reading the following I developed a working solution:
- Moving a custom configuration group to a separate file
- An error occurred creating the configuration section handler
- How to get the values of a ConfigurationSection of type NameValueSectionHandler
Updated App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="globalSettings" type="System.Configuration.AppSettingsSection"/>
</configSections>
<appSettings file="AppSettings.config"/>
<connectionStrings configSource="ConnectionStrings.config"/>
<globalSettings file="GlobalSettings.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Updated GlobalSettings.config:
<globalSettings>
<add key="key" value="keyValue" />
</globalSettings>
Function code:
private void RetrieveKey()
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection globalSettingSection = (AppSettingsSection)config.GetSection("globalSettings");
key = globalSettingSection.Settings["key"].Value;
}
I have found that you need to define the new section in the App.config rather than in the config file you're creating. Also the type of the section is important. You can use one of the following:
type="System.Configuration.AppSettingsSection"
type="System.Configuration.IgnoreSectionHandler
type="System.Configuration.NameValueSectionHandler"
I found type="System.Configuration.AppSettingsSection"
to be the most useful as it makes the config most like the AppSettings.config file.
Please let me know if you have any changes that would make things more efficient.
Related Topics
How to Seed a Random Class to Avoid Getting Duplicate Random Values
How to Read and Modify Ntfs Alternate Data Streams Using .Net
How to Decode a Url Parameter Using C#
How to Store User Settings for a .Net Application
When to Dispose Cancellationtokensource
Http Error 500.30 - Ancm In-Process Start Failure
When Is It Better to Use String.Format VS String Concatenation
How to Download HTML Source in C#
How to Check for Permissions to Write to a Directory or File
Correct Way to Override Equals() and Gethashcode()
How to Round to the Nearest 0.5
Opensubkey() Returns Null for a Registry Key That I Can See in Regedit.Exe
Coercing Floating-Point to Be Deterministic in .Net
Fast Converting Bitmap to Bitmapsource Wpf
What Are the True Benefits of Expandoobject