开发者

How to create generic web.config file for different web servers in .net web application?

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?


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.


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

web.config files can override some settings from it.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜