Shared Environment Configuration
In a situation, where you want to keep and maintain configurations for each environment (DEV, UAT and PROD etc) separately in a centralized location, attach them to your application and/or switch them at run-time, all these can be done using Cinchoo configuration framework. This is the first of the series of articles about SharedEnvironment in this blog.
Cinchoo framework discover this file (SharedEnvironment.config) in the application base directory if not specified explicitly. It can be overridden by couple of ways
- appSettings – If you have a file contain shared environment xml in a different location, you can specify them here. Below is the sample
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AppEnvironment" value="DEV" />
<add key="SharedEnvironmentConfgiFilePath" value="c:\Temp\NewSharedEnvironments.config"/>
</appSettings>
</configuration>
- Run-Time – In cases where SharedEnvironment xml is not in a file or stored in different sources (Database, WebService etc), you can retrieve them and pass it to the framework by subscribing to ChoApplication.GetSharedEnvironmentConfigXml callback. Below is sample
class Program
{
static void Main(string[] args)
{
ChoApplication.GetSharedEnvironmentConfigXml = new Func<string>(() =>
{
string xml = null;
//Go to source and retrieve the xml
return xml;
});
}
}
Your application can be configured to use particular environment by specifying at appSettings as below
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AppEnvironment" value="DEV" />
<add key="SharedEnvironmentConfgiFilePath" value="c:\Temp\NewSharedEnvironments.config"/>
</appSettings>
</configuration>
Now lets walk over the specification of SharedEnvironment.Config file.
<?xml version="1.0"?>
<configuration>
<sharedEnvironment baseAppSharedConfigDirectory="C:\Config" defaultEnvironment="DEV">
<environment name="DEV" freeze="true" appFrxFilePath="DEVConfig" >
<machine>WNYC12D10101</machine>
<machine>WNYC12D10102</machine>
<machine>WNYC12D10103</machine>
<machine>WNYC12D10104</machine>
</environment>
<environment name="PROD" appFrxFilePath="C:\Config\PROD\TestApp.config.txt" freeze="true" >
<machine>SNYC12D10101</machine>
<machine>100.39.191.175</machine>
</environment>
<environment name="UAT" >
<machine>WNYC1108054621</machine>
<machine>SNYC*</machine>
</environment>
</sharedEnvironment>
</configuration>
- baseAppSharedConfigDirectory – A configuration directory used by a environment whose appFrxFilePath is not specified or it contains relative file path. In the above samples, environment ‘UAT’ uses this directory to read / store configuration. If not specified, it will be defaulted to application binary base directory.
- defaultEnvironment – An environment used by any host which is not listed in this xml.
Each environment can be created using ‘environment’ element. In the above sample, we have ‘DEV’, ‘PROD’, and ‘UAT’ environments.
- name – Name of the environment, mandatory.
- freeze – true, all the hosts belongs to the environment locked. Can not be overridden by specifying it in appSettings/appEnvironment. false, it can be overridden. Default false. Optional.
- appFrxFilePath – A file / directory path. Either absolute / relative path. In case of relative path, framework resolves the path in relative to ‘baseAppSharedConfigDirectory’ path. In the sample above, for the ‘DEV’ environment, this path resolved to ‘C:\Config\DEVConfig’. If missing/not specified, the path will be the environment ‘Name’ under ‘baseAppSharedConfigDirectory’. In the sample above, the ‘UAT’ environment will resolve this path to ‘C:\Config\UAT’. It is optional.
Then you can bind hosts to any environment using ‘machine’ element under ‘environment’. Host can be either MachineName or IP Address. Also the those host names can contain wild card characters as well.
Ex: SYNC*, WNYC1002340? etc