Configuration files containing environment specific settings

We have multiple environments in my organization (Dev, QA, Stage, Prod, Disaster Recovery) and I would like my app / web.config files to not contain any environment specific details in URIs, connection strings, etc. My preferred approach would be to do the substitution at runtime, like instead:

<endpoint address="http://MyDevWebServer/SomeService" binding="basicHttpBinding"...

I would rather have something like this:

<endpoint address="http://{Env:WebServerName}/SomeService" binding="basicHttpBinding"...

It's just for the config settings read from the classes I manage (just put in a wrapper that replaces {Env: WebServerName} with the actual server name for that environment), but I also want this replacement to be done when the settings are read from classes I have no control over (e.g. WCF, NHibernate, etc.). Does anyone know a way to inject such replacement logic. I imagine an AOP approach might be possible, but personally don't have much experience with AOP frameworks.

+2


source to share


6 answers


You should do this by inferring a custom class from ApplicationSettingsBase that provides this replacement.



I recommend reading up on Application Settings Architecture . It walks you through the process of creating your own customization architecture, including setting up your own handler that can read from any source or do other work in the process.

+1


source


One approach is to use ConfigSource for the configuration section (appSettings, connectionStrings, etc.). See http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx for details . Thus, web / app.config can contain basic information, and unique environment settings can be set in the file for that environment.



+1


source


We have a post-build nant script that uses XPath to define and replace environment-specific values ​​depending on which environment we are targeting. All configuration values ​​for the development environment are default. This adds a short delay to the build process, but has proven to be a very reliable solution in practice, i.e. no confused or crossed environment settings.

+1


source


We moved our setup to the database and didn't deal with it, see here.

0


source


I agree that such a replacement would be desirable and requested this feature from Microsoft Connect .

Not much interest from Microsoft though :(

0


source


While you are trying to find a way to do the runtime replacement, I would guess that in this case it would be a better solution to replace the build time. For example, we use nant task xmlpoke to replace config values ​​at build time. You can also try replacing the web.config section in web deployment projects or the XmlMassUpdate Task from MSBuild Community Tasks ( example ).

0


source







All Articles