c# - .NET configuration with multiple instances of a set of parameters -
i have application can connect several servers. actual number of servers not known until runtime,and may change day day. takes several actual parameters define server.
i'm trying configure application using .net support application configurations.
the configuration file looks like:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <sectiongroup name="usersettings" type="system.configuration.usersettingsgroup, system, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" > <section name="server" type="system.configuration.singletagsectionhandler" allowexedefinition="machinetolocaluser" requirepermission="false" /> </sectiongroup> </configsections> <usersettings> <server name="washington"> <add name="host" value="abc.presidents.com"/> <add name="port" value="1414"/> <add name="credentials" value="george"/> </server> <server name="adams"> <add name="host" value="def.presidents.com"/> <add name="port" value="1419"/> <add name="credentials" value="john"/> </server> <!--insert more server definitions here --> </usersettings> </configuration>
and tried read code (the writelines diagnosing problem. go away when/if works.):
try { var execonfiguration = configurationmanager.openexeconfiguration(configurationuserlevel.peruserroamingandlocal); console.writeline(execonfiguration); var usersettings = execonfiguration.getsectiongroup("usersettings"); console.writeline("{0}: {1}", usersettings, usersettings.name); var sections = usersettings.sections; console.writeline("{0}: {1}", sections, sections.count); foreach (configurationsection section in sections) { console.writeline("{0}", section); // todo here's capture information server } } catch (system.exception ex) { console.writeline("exception: {0}", ex.message); }
this throws exception foreach , produces following output:
system.configuration.configuration system.configuration.usersettingsgroup: usersettings system.configuration.configurationsectioncollection: 1 exception: sections must appear once per config file. see topic <location> exceptions.
if remove second server, leaving washington, "works" producing output:
system.configuration.configuration system.configuration.configurationsectiongroup: usersettings system.configuration.configurationsectioncollection: 1 system.configuration.defaultsection
i've tried other variations on theme (nested sectiongroups, etc.) without finding solution. various tutorial examples i've found seem want me create separate class each server. impractical, , should unnecessary since servers created equal.
questions:
- does system.configuration support concept of collection of sets of related settings (like array of structures)?
- if so.. how?
- if not.. there way store persistent configuration information support concept, or going have roll-my-own?
progress report
based on partial answer robert mckee modified xml so:
<section name="servers" type="system.configuration.defaultsection, system.configuration, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a"
and
<servers> <add name="washington" host="abc.presidents.com" port="1414" credentials="george"/> <add name="adams" host="def.presidents.com" port="1419" credentials="john"/> <!--insert more server definitions here --> </servers>
this improvement 1 fatal problem. can see changed type
attribute of section
element name class ystem.configuration.defaultsection
defaultsection
succeeds @ reading configuration information (i think, @ least not complain.) not expose way access information read!
therefore need use other type of *section
class. microsoft provides 95 classes derived configurationsection
base class, of them except defaultsection
, clientsettingssection
appear targeted @ special cases (urls, database connection strings, date , times, etc...) clientsettingssection
won't read servers section -- complaining <add/>
not valid nested element.
so, bottom line i'm going have write custom configurationsection
handle these settings. if working i'll add answer ultimate solution (unless provide better answer first.)
haven't worked custom config sections, purely logical point of view, isn't more appropriate:
<usersettings> <servers> <add name="washington" host="abc.presidents.com" port="1414" credentials="george"/> <add name="adams" host="def.presidents.com" port="1419" credentials="john"/> <!--insert more server definitions here --> </servers> <!-- insert more user settings here --> </usersettings>
you can other ways, if want collection of "server", must in grouping element "servers". can't have multiple "server" in parent element can contain other types of children. tag within group "add".
in case "system.configuration.singletagsectionhandler" not correct type.
Comments
Post a Comment