app.config

  • Thread starter Thread starter Guinness Mann
  • Start date Start date
G

Guinness Mann

I'm working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I've been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
'ConfigurationSettings.AppSettings["strCon"]'. How would that change?

Thank you,

-- Rick
 
Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct 'environments' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuration>

<configSections>
<section name="myClass1Stuff"
type="System.Configuration.DictionarySectionHandler, System" />
<sectionGroup name="environments">
<section name="work"
type="System.Configuration.SingleTagSectionHandler, System" />
<section name="home"
type="System.Configuration.SingleTagSectionHandler, System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environment" value="work" />
</appSettings>

<myClass1Stuff>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments>
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySectionHandler you can easily change the key
or value in your section. I've derived from DictionarySectionHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any 'per
environment' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionString, printer, messageQueue and such that
are distinct per environment.

You then need to use System.Configuration.ConfigurationSettings.GetConfig to
get your section, which returns an object based on the type of section
handler defined for that section (usually HashTable, but can be other object
types.

See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/d...de/html/cpconconfigurationsectionhandlers.asp

and:
http://msdn.microsoft.com/library/d...ref/html/gngrfconfigurationsectionsschema.asp

Also read about the System.Configuration.ConfigurationSettings class and
other classes in the System.Configuration namespace.

If you define a distinct enough custom section you could put it in the
machine config, however I would put it under something other than
appSettings, as all applications can read appSettings (technically All .NET
applications can read all sections of machine config). If I were to put a
custom section in machine.config I would more than likely use the namespace
for my assembly as the custom section name. For the above I would define a
custom section to hold which environment I was using, then

Hope this helps
Jay


Guinness Mann said:
I'm working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I've been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
'ConfigurationSettings.AppSettings["strCon"]'. How would that change?

Thank you,

-- Rick
 
Jay,

Perhaps I'm dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven't figured out where, yet :-) )

You say:
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).

Actually, that sounds perfect to me. At work, all applications need to
access the same SQL Server. Likewise at home. I can see where in a
more complex environment with more servers it would be a problem.

So then, when I call System.Configuration.AppSettins["section"] does it
search for "section" in both places: app.config and machine.config?

-- Rick
 
Rick,
So then, when I call System.Configuration.AppSettins["section"] does it
search for "section" in both places: app.config and machine.config?

ConfigurationSettings reads machine.config first, then it "merges" what is
in app.config. I understand that it physically reads the config files once,
and caches the results returned from ConfigurationSettings.GetConfig,
avoiding extra overhead of continually reading & searching the files.

This merging allows app.config to override what is in machine.config. It is
also part of the reason that every thing in appSettings is normally <add>,
it can be <remove> or <clear>

What I tried to say at the bottom of my response is if you use
machine.config DO NOT USE appSettings. Define your own custom section,
similar to the "environments" section in my example, just make sure you name
the section for the namespace of your assembly. Also make sure this name
follows the .NET guide lines (company.TehcnologyName).

http://msdn.microsoft.com/library/d...enref/html/cpconnamespacenamingguidelines.asp

To minimize interfering with future .NET upgrades and other class libraries
that may be installed.

Hope this helps
Jay


Guinness Mann said:
Jay,

Perhaps I'm dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven't figured out where, yet :-) )

You say:
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).

Actually, that sounds perfect to me. At work, all applications need to
access the same SQL Server. Likewise at home. I can see where in a
more complex environment with more servers it would be a problem.

So then, when I call System.Configuration.AppSettins["section"] does it
search for "section" in both places: app.config and machine.config?

-- Rick


Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct 'environments' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuration>

<configSections>
<section name="myClass1Stuff"
type="System.Configuration.DictionarySectionHandler, System" />
<sectionGroup name="environments">
<section name="work"
type="System.Configuration.SingleTagSectionHandler, System" />
<section name="home"
type="System.Configuration.SingleTagSectionHandler, System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environment" value="work" />
</appSettings>

<myClass1Stuff>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments>
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySectionHandler you can easily change the key
or value in your section. I've derived from DictionarySectionHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any 'per
environment' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionString, printer, messageQueue and such that
are distinct per environment.
 
[This followup was posted to microsoft.public.dotnet.general and a copy
was sent to the cited author.]

ConfigurationSettings reads machine.config first, then it "merges" what is
in app.config. I understand that it physically reads the config files once,
and caches the results returned from ConfigurationSettings.GetConfig,
avoiding extra overhead of continually reading & searching the files.
...
Hope this helps

Thanks, Jay. That was just right!

-- Rick
 
Back
Top