exception - Unable to read from config file in console app in c# -
exception - Unable to read from config file in console app in c# -
i trying read 2 variables app.config file in project in visual studio 2012. exception of : "configuration scheme failed initialize". ideas wrong here?
here app.config file
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> <configurationsections> <section name ="connectionconfig" type="connectionmanager.myconnectionconfigurationsection" /> </configurationsections> <connectionconfig portnumber="7777"/> <connectionconfig hostname="localhost" /> </configuration>
the c# file:
namespace connectionmanager { public class myconnectionconfigurationsection : system.configuration.configurationsection { [configurationproperty("portnumber")] public string portnumber { { homecoming (string)this["portnumber"]; } set { this["portnumber"] = value; } } [configurationproperty("hostname")] public string hostname { { homecoming (string)this["hostname"]; } set { this["hostname"] = value; } } } public static class connectionapplication { public static myconnectionconfigurationsection config { get; internal set; } public static void initialize() { seek { config = configurationmanager.getsection("connectionconfig") myconnectionconfigurationsection ; } grab (exception ex) { console.writeline(ex.message.tostring()); } } } class programme { static string portnumber; static string hostname; static void main(string[] args) { connectionapplication.initialize(); portnumber=connectionapplication.config.portnumber; console.writeline(portnumber); hostname = connectionapplication.config.hostname; console.writeline(hostname); } } }
it seems not beingness able initialize only...when dump exception, exception says : configuration scheme failed initialize. suggestions doing wrong
check code again:
<configurationsections> <section name ="connectionconfig" type="connectionmanager.myconnectionconfigurationsection" /> </configurationsections> <connectionconfig portnumber="7777"/> <connectionconfig hostname="localhost" />
it's <configsections>
why declare twice custom section? <connectionconfig portnumber="7777" hostname="localhost" />
right way of using custom section. <section name ="connectionconfig" type="connectionmanager.myconnectionconfigurationsection" />
on type
attribute should provide total assembly qualified name of class. is, if assembly called connectionmanager
should configure connectionmanager.myconnectionconfigurationsection, connectionmanager
. btw, in opinion, if you're going configure 2 simple settings, wouldn't utilize custom configuration section (this overkill). why don't utilize appsettings
built-in on .net configuration model?
<appsettings> <add key="connectionmanager:host" value="localhost" /> <add key="connectionmanager:port" value="7777" /> </appsettings>
and you'll access these parameters way:
string host configurationmanager.appsettings["connectionmanager:host"]; int port = int.parse(configurationmanager.appsettings["connectionmanager:port"]);
c# exception visual-studio-2012 configuration app-config
Comments
Post a Comment