views:

73

answers:

5

I want to create a generic web.config file for different web servers in VB.NET. So, depending on the server configuration requirements, applications can retrieve all values from that generic configuration file.

Is this possible? How would I do this?

+2  A: 

This is just a random idea, it may not fit your needs though. You could create a configuration section for each server named by the name of the server. Create a helper class for reading configuration values that checks for any values in the section named after the server's name first, if it doesn't exist read it from a default configuration section.

I'm still not sure if this would be a wise decision, its just an option.

Smelch
+1  A: 

Technically, there's the machine.config which includes settings that apply to the entire machine.

web.config files can override some settings from it.

Michael Stum
A: 

For everything that stays the same, use the a single web.config.

For everything that changes, use a reference to an external file.

<configuration>
   <appSettings file="ExternalWeb.config">
      <add key="MyKey" value="MyValue" />
   </appSettings>
   ...
</configuration>

http://www.devx.com/vb2themax/Tip/18880

This way when things change in the main web.config, few things must be updated.

You may also consider using templates and code generation techniques to generate a web.config for each server.

MatthewMartin
This only works for `appSettings`.
John Saunders
but, suppose i have 2 different web server and for webserver1 how can i query config file and get settings for that one and diffenrent settings for webserver2?
@John. Not entirely true many tags have an external file attribute, also works for connectionStrings ,e.g. configSource="web_settings.config", so does profile. http://weblogs.asp.net/fmarguerie/archive/2007/04/26/using-configsource-to-split-configuration-files.aspx
MatthewMartin
@user390771 I'm not following where you are going anymore. The typical scenario is that you have a dev, test and production server each with a slightly different web.config file, but with most of the content the same. Each app has only one config file.You may have in mind a resource that is accessible from from several resources simutaneously. That would require storing settings in a database. You can implement that manually as long as each of your servers has a web.config file with a suitable connection string.
MatthewMartin
@Matthew: that's `configSource`, not `file`.
John Saunders
@John Yes you are correct. Those strings are not equal.
MatthewMartin
+1  A: 

How about a "mode" appsetting key/value. This "mode" can be set to "dev", "testing", "prod", etc. Then, set the mode of the current configuration file and prefix all the settings that would change with the mode.

Example:

<add key="mode" value="test" /> <!-- possible values: dev, test, prod -->

<add key="dev.dbconnstr" value="data source=DB;userid=ABC;password=DEF" />
<add key="test.dbconnstr" value="data source=DB;userid=ABC;password=DEF" />
<add key="prod.dbconnstr" value="data source=DB;userid=###;password=###" />

Then, use a configuration class to read the setting depending on the mode.

Example:

mode = ConfigurationManager.AppSettings("mode");
CongifurationManager.AppSettings(mode + ".dbconnstr");

Doing it this way, you can have the same config file deployed to all servers, and never have to worry about tweaking each server (except of course updating the "mode" value when deploying). I would also recommend not saving the production credentials in the other configuration files, instead replace it with a placeholder.

NJITBEN
Are people still using database passwords in config files?
oɔɯǝɹ
That is clever. Another variation would be to use Machine name instead of mode.
MatthewMartin
@remco Yes because it isn't reasonable to ask most DBAs and server admins to configure delegation to allow for a double hop of windows credentials (for asp.net to sql on a different box scenarios). I've asked people much smarter than me to do so and they gave up.
MatthewMartin
A: 

You could create a deployment script in something like nant which loads in a web.config containing placeholders for the configuration options. This could then replace the placeholders for the appropriate environments.

James O'Sullivan