Application configuration file error at runtime - c#

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.

Related

Base class for view with RazorGenerator precompiled views

I have console project with some .cshtml files stored in Resources folder, which is built into .cs files with Razorgenerator.MsBuild nuget package.
I need to replace WebViewPage view base class for generated view, but adding web.config file with pageBaseType="ConsoleApp.MyViewBase" option set or changing location of files in different ways did not help me and no any other ways where found.
View class is always generated as:
public partial class _Resources_Index_cshtml :
System.Web.Mvc.WebViewPage<ConsoleApp.ModelClass> { ... }
Hope, there is some way to confgure the class globally for all views, without adding directives at the beginning of every .cshtml file.
It looks that that setting base view in web.config works in latest RazorGenerator.
So update your RazorGenerator package and try to have something like this in web.config:
<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.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="WebApplication2.Views.BaseView2">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
In my case I was adding precompilation (with RazorGenerator.Mvc and RazorGenerator.MsBuild packages) to old project and it didn't work. The problem was that I had Version=2.0.0.0 in my configSections project and had to change it to Version=3.0.0.0.
If someone finds it helpful, there is a nuget package for this by me. It is same Razorgenerator.MsBuild but with replacing System.Web.Mvc.WebViewPage by Custom.System.Web.Mvc.WebViewPage. So if you want to use custom WebViewPage, install the package and add you class named WebViewPage in namespace Custom.System.Web.Mvc. Like:
namespace Custom.System.Web.Mvc
{
public abstract class WebViewPage<T>
{
...
}
}

configuration system failed to initialize ==> unrecognized configuration section

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>

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