Monthly Archives: May 2012

Cinchoo – Configuration framework – SharedEnvironment, part 1

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

  • appFrxSettings – 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" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
	</configSections>
	<appFrxSettings appFrxFilePath="" sharedEnvironmentConfigFilePath="" appEnvironment="QA" />
</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 appFrxSettings as below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
	</configSections>
	<appFrxSettings appFrxFilePath="" sharedEnvironmentConfigFilePath="c:\Temp\NewSharedEnvironments.config" appEnvironment="QA" />
</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 appFrxSettings/appEnvironment. 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.
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

Cinchoo – All possible valid INI name values, Part 8

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’m going to show you all the possible valid INI name values can be given in the INI document

[PRODUCT]
ENVIRONMENT                                                               ;No Value specified
VERSION = 1.002                                                           ;Valid Value specified
ADDRESS = 10 River Road, \
          Orlando, \
          FL 100230.                                                      ;Multi-Line value specified
CONNECTION_STRING1 = "PROVIDER=SQLServer;UserName=xxxx;Password=yyyyy"    ;Value with ';' characters
CONNECTION_STRING2 = "PROVIDER=Oracle;UserName=xxxx;
                      Password=yyyyy"                                     ;Multi-Line Value with ';' characters

 


Cinchoo – INI Parser Settings, Part 7

ChoIniDocument

Reading and writing INI files using Cinchoo framework can be controlled through global INI settings. These settings are available for your to edit at ChoIniSettings.xml file in your application binary folder. It will be created automatically when you run your application, if not exists. Here is the sample INI settings file looks like

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <iniSettings nameValueSeperator="=" commentChars=";#" ignoreValueWhiteSpaces="false" />
</configuration>

Where

  • nameValueSeperator – A character separate the key-value under section. Default value is ‘=’.
  • commentChars – List of INI comment character parser used to identify the comments. Default value is ‘;#’.
  • ignoreValueWhiteSpaces – true, to trim any white-spaces surrounded the value. Otherwise, preserve the white space with the value if any.

Cinchoo – Reading all INI sections, Part 7

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’ll show you how you can read all the available INI section inside a INI document.. For a sample INI file below

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS=10 River Road, \
        Orlando, \
        FL 100230.

[SOFTWARE]
OS1=Windows
ENVIRONMENT=PRODUCTION

Reading all the INI sections can be done as below

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        foreach (ChoIniSectionNode iniSectionNode in iniDocument.Sections)
        {
            Console.WriteLine(String.Format("Key-Values for {0} Section...", iniSectionNode.Name));
            foreach (KeyValuePair<string, string> keyValue in iniSectionNode.KeyValues)
                    Console.WriteLine("{0} = {1}", keyValue.Key, keyValue.Value);
        }
    }

Cinchoo – Reading all INI section key values, Part 6

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’ll show you how you can read all the key-values from a INI section. For a sample INI file below

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS=10 River Road, \
        Orlando, \
        FL 100230.

Reading all key-values for PRODUCT section can be done as below

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        ChoIniSectionNode productIniSectionNode;
        if (iniDocument.TryGetSection("PRODUCT", out productIniSectionNode))
        {
            foreach (string key in productIniSectionNode.Keys)
                Console.WriteLine("{0} = {1}", key, productIniSectionNode[key]);
        }
    }

OR

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        ChoIniSectionNode productIniSectionNode;
        if (iniDocument.TryGetSection("PRODUCT", out productIniSectionNode))
        {
            foreach (KeyValuePair<string, string> keyValue in productIniSectionNode.KeyValues)
                Console.WriteLine("{0} = {1}", keyValue.Key, keyValue.Value);
        }
    }

Follow

Get every new post delivered to your Inbox.