configuration system failed to initialize ==> unrecognized configuration section - c#

I don't even know if I can do what I'm attempting but I've imported forms from several projects and added references to those projects. Each project has a different set of connection strings and I'm trying to get them to coexist in App.config where I can filter by SECTION (Users select connections from comboboxes). I am hoping I can do this by implementing ConfigSections. If it's doable I obviously don't know how.
Attached is my App.config. I'm getting the error 'configuration system failed to initialize' and when I drill into the detail it says 'unrecognized configuration section amSettings
Is what I'm trying to do possible? If so, what do I need to correct?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ApplicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="amSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

Change the section name from amSettings.Properties.Settings to amSettings and cbSettings.Properties.Settings to cbSettings
e.g.
`<section name="amSettings" `
Here is a comprehensive example:
If you change your config file to this:
<configSections>
<section name="amSettings"
type="System.Configuration.AppSettingsSection"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.AppSettingsSection"
requirePermission="false"/>
</configSections>
<amSettings>
<add key="ABC" value="DEF"/>
</amSettings>
Then you can access the key ABC using this code:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
var a = appSettingSection.Settings["ABC"].Value;

The solution was two things. Change section name as user469104 recommended and wrapping sections in Group Name.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ApplicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="amSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<ApplicationSettings>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
</ApplicationSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

Related

Application configuration file error at runtime

The context :
I have an winform application which is launch by users on the local network (path: \\file-01\appsdeploy$\MyApp )
When I launch the app, it's ok it worked well. But on some other computer it does not work. I have this exception :
An error occurred creating the configuration section handler for
MySection: Request failed.
The declaration of the section in the app.config file is :
<configSections>
<section name="MySection" type="MyGenerator.Config.MySection, MyGenerator"/>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MySectionGenerator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
Now the section :
<MySection>
<Addresses>
<add Name="1" Text="ADDRESS ONE"/>
<add Name="2" Text="ADDRESS TWO"/>
<add Name="3" Text="ADDRESS THREE"/>
<add Name="4" Text="ADDRESS FOUR"/>
<add Name="5" Text="ADDRESS FIVE"/>
<add Name="6" Text="ADDRESS SIX"/>
<add Name="7" Text="ADDRESS SEVEN"/>
</Addresses>
</MySection>
NOTE : If I copy the executable and the config file on the local computer, it works fine.
Have you got an idea, a clue... ?
Check with allowExeDefinition=machinetoapplication option.

Html.ActionLink reference error MVC4 in Areas

I've created an Area in my app called "Admin" and when I attempt to use #html.actionlink() to build some menu items I'm met with a reference error.
system.web.WebPages.Html.HtmlHelper does not contain a definition for ActionLink and the best extension method overload System.Web.Html.LinkExtensions.ActionLink(System.Web.HtmlHelper,string, string, string) has some invalid arguments.
This is the line of code that's generating the error.
#Html.ActionLink("Posts", "Index", new { area = "admin" });
I've googled around and attempted adding using system.web.mvc.html; with no benefit or change in behavior. I've checked the web.config file for the area and confirmed that both system.web.mvc and system.web.mvc.html are included in the namespaces, also to no avail.
You are using actionlink wrongly.
Below is the correct usage of actionlink(i m taking example of any arbitrary actionlink)
Html.ActionLink(article.Title, // <--Link Text
"Item", // <-- ActionMethod
"Login", // <-- Controller Name.
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments
)
This uses the following method ActionLink signature:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object values,
object htmlAttributes)
The error is coming in your question because 3rd overload of Actionlink is controller name.
#Html.ActionLink("// Link Text //", "// Action Name //",// controller name //, new { area = "admin" });
You are missing a web.config in your /Areas/Admin/Views folder. Just create the basic one like below, the key part being the added namespaces:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="Club.Web" />
<add namespace="Club.Web.Helpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>

what will be the config section when adding a section to web.config?

I want to add domain key and value to my web.config for ldap authentication but when adding
<domain>
<add key="don" value="fffT"/>
<add key="LD" value="LDAP://n.tt.sg/DC=ttt,DC=xx,DC=exxxx,DC=sg"/>
</domain>
it shows the error could not find schema information of value and key. What should i write instead of
<section name="domain" type="System.Configuration.NameValueFileSectionHandler,System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
Do like this
<configuration>
<appSettings>
<add key="don" value="fffT"/>
<add key="LD" value="LDAP://n.tt.sg/DC=ttt,DC=xx,DC=exxxx,DC=sg"/>
</appSettings>
</configuration>
Put Key and values inside appSettings Section of web.confing
System.Configuration.NameValueFileSectionHandler is from System assembly, not System.Web.Extensions.
type="System.Configuration.NameValueFileSectionHandler, System, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

Multiple AppSettings files, is it possible?

I want to create 3 AppSettings config files:
Database.config
Messages.config
Global.config
And after add in my App.config:
<appSettings file="Database.config" />
<appSettings file="Messages.config" />
<appSettings file="Global.config" />
But when I try to access a key that there is in one of three files with the ConfigurationManager, I got the following error:
Configuration system failed to initialize. Sections must only appear once per config file.
I cannot have more than one AppSettings config file?
You can't have more than one appsettings because that's the name of a section. You can add a new section though that uses the same kind of section definition as appsettings. E.g.,
<configuration>
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
....
<DatabaseConfig>
<add key="Whatever" value="stuff"/>
</DatabaseConfig>
</configuration>
Code for separate file:
Web.config:
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="MessageConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="GlobalConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<DatabaseConfig configSource="database.config">
</DatabaseConfig>
<MessageConfig configSource="message.config">
</MessageConfig>
<GlobalConfig configSource="global.config">
</GlobalConfig>
database.config:
<DatabaseConfig>
<add key="Name" value="ServerName" />
</DatabaseConfig>
etc...
Can be accessed via code like this:
var databaseConfiguration = (NameValueCollection)ConfigurationManager.GetSection("DatabaseConfig");
string name = databaseConfiguration["Name"];

can't read variables out of app.config

what I have in app.config is this
<?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="Porject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<applicationSettings>
<Porject.Properties.Settings>
<setting name="PhotoLocation" serializeAs="String">
<value>.\photos\</value>
</setting>
</Porject.Properties.Settings>
</applicationSettings>
<connectionStrings>
<add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>
</configuration>
and this is how I call the PhotoLocation:
string s = ConfigurationManager.AppSettings["PhotoLocation"];
When I make a request to get the connectionstring, there is no problem and it works fine; but when I requst the PhotoLocation it returns null.
Also the ConfigurationManager.AppSettings.Count returns 0.
Does anyone have an idea on what I'm doing wrong?
simply add this in your App config file
<appSettings>
<add key="PhotoLocation" value=".\photos\"/>
<appSettings>
ConfigurationManager.AppSettings reads (as the name might suggest) the AppSettings block of your configuration.
Given you've created your own section, you want to use var section = ConfigurationManager.GetSection("Porject.Properties.Settings") and read the values from that section.

Categories

Resources