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.
SharedEnvironment.config file is a xml based configuration file shared by many applications. It contains configuration entries which controls the location of the application configuration locations and optional machines list for each environment etc.
Cinchoo framework discover this file (SharedEnvironments.xml) in the application base directory if not specified explicitly. It can be overridden by couple of ways
- appFrxSettings – If the shared environment xml file is in a different location, you can specify them at sharedEnvironmentConfigFilePath attribute in App.config file as below
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" /> </configSections> <appFrxSettings sharedEnvironmentConfigFilePath="" appEnvironment="QA" /> </configuration>
- Run-Time – In case 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; }); } }
<?xml version="1.0"?> <configuration> <sharedEnvironment baseAppSharedConfigDirectory="C:\Config" defaultEnvironment="DEV"> <environment name="DEV" comment="DEV Environment" appFrxFilePath="DEVConfig" > <machine>WNYC12D10101</machine> <machine>WNYC12D10102</machine> <machine>WNYC12D10103</machine> <machine>WNYC12D10104</machine> </environment> <environment name="PROD" comment="PROD Environment" appFrxFilePath="C:\Config\PROD\TestApp.config.txt" freeze="true" > <machine>SNYC12D10101</machine> <machine>100.39.191.175</machine> </environment> <environment name="UAT" comment="UAT Environment" > <machine>WNYC1108054621</machine> <machine>SNYC*</machine> </environment> </sharedEnvironment> </configuration>
- baseAppSharedConfigDirectory – A configuration directory used by an environment whose appFrxFilePath is not specified or it contains relative file path. In the above sample, the ‘DEV’ environment will create or use the ‘C:\Config\DEV’ directory to consume configuration files. The environment ‘UAT’ uses ‘C:\Config\UAT’ directory to read / store configuration. If not specified, it will be defaulted to application binary base directory.
- defaultEnvironment – An environment used by the host which is not listed in this xml. In this sample, the ‘DEV’ environment will be used by the host not attached to any environment.
- name – Name of the environment, mandatory.
- freeze – true, all the hosts belongs to the environment locked. The applications hosted on these machines are locked to use this environment. By no means, it can be overridden via App.Config file in appFrxSettings/appEnvironment section. False, it can be overridden. Default value is 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.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" /> </configSections> <appFrxSettings appEnvironment="QA" /> </configuration>