How to access Service Configuration settings in Windows Azure Startup Task

In this example I will show you how you can access Service Configuration settings in Windows Azure Startup Task. The startup task is Startup.cmd which is launching another batch file name showconfvalues.cmd.

 

The Startup.cmd is as below:

cd /d "%~dp0"

Start /w showconfvalues.cmd

 

The showconfvalues.cmd is as below:

@echo on

@echo Your STRING 1 is: %CONFSTRING1%

@echo Your STRING 2 is: %CONFSTRING2%

 

The Windows Azure Application Service Definition file is as below:

 <?xml version="1.0" encoding="utf-8"?>
 <ServiceDefinition name="AzureCmdApp" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
 <WorkerRole name="CmdWorkerRole" vmsize="ExtraSmall">
 <Runtime>
 <Environment>
 <Variable name="CONFSTRING1">
 <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='SettingXYZ']/@value" />
 </Variable>
 <Variable name="CONFSTRING2">
 <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='SettingABC']/@value" />
 </Variable>
 </Environment>
 <EntryPoint>
 <ProgramEntryPoint commandLine="Startup.cmd" setReadyOnProcessStart="true" />
 </EntryPoint>
 </Runtime>
 <Endpoints>
 <InputEndpoint name="HttpIn" protocol="tcp" port="8999" />
 </Endpoints>
 <ConfigurationSettings>
 <Setting name="SettingXYZ" />
 <Setting name="SettingABC" />
 </ConfigurationSettings>
 </WorkerRole>
 </ServiceDefinition>
 

The Windows Azure Application Service Configuration file is as below:

 <?xml version="1.0"?>
 <ServiceConfiguration xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" serviceName="AzureCmdApp" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
 <Role name="CmdWorkerRole">
 <Instances count="1" />
 <ConfigurationSettings>
 <Setting name="SettingXYZ" value="This is Setting 1 XYZ" />
 <Setting name="SettingABC" value="This is Setting 2 ABC" />
 </ConfigurationSettings>
 </Role>
 </ServiceConfiguration>
 

You can launch this application from Windows Azure SDK Command prompt in the Administrator mode as below:

 

When running this application you will see the results showing your configuration values as below:

 Your STRING 1 is: This is Setting 1 XYZ

Your STRING 2 is: This is Setting 2 ABC

 

 Project Files:

  • build.cmd
  • pack.cmd
  • run.cmd
  • ServiceConfiguration.cscfg
  • ServiceDefinition.csdef
  • CmdWorkerRole <FOLDER>
    • Startup.cmd 
    • showconfvalues.cmd

 

Study more about xPath Values in Windows Azure

 

Download this this project from github as below: