Sharing configuration settings between Windows Azure roles

Posted by theo.spears on Simple Talk See other posts from Simple Talk or by theo.spears
Published on Wed, 16 Mar 2011 16:45:00 GMT Indexed on 2011/03/17 0:17 UTC
Read the original article Hit count: 377

Filed under:

If you are working on a medium-large Windows Azure project it's likely it will involve more than one role, for example separate web and worker roles. Unfortunately although all the windows azure configuration settings are stored in a single cscfg file, there is no way to share configuration settings between multiple roles. This means you have to duplicate common settings like connection strings across all your roles. There is an open Connect issue about this topic, but Microsoft have not said when they will fix it.

In the mean time I've put together a dirty dirty hack cunning workaround that creates a fake role containing your shared configuration settings, and copies it to all roles as part of the build process. Here's how you set it up:

1. Download the zip file attached to this post, and unzip it into the folder containing your Azure project (not your solution folder).

2. Edit your csdef and cscfg files to include the placeholder project

ServiceDefinition.csdef

<?xml version="1.0" encoding="utf-8"?> 
<ServiceDefinition
name="AzureSpendNotifier"
http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition%22">http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="GLOBAL">
<ConfigurationSettings>
<Setting name="ExampleSetting" />
</ConfigurationSettings>
</WorkerRole>
<WorkerRole name="MyWorker">
<ConfigurationSettings>
</ConfigurationSettings>
</WorkerRole>
<WebRole name="MyWeb">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="WebEndpoint" endpointName="WebEndpoint" />
</Bindings>
</Site>
</Sites>
<ConfigurationSettings>
</ConfigurationSettings>
</WebRole>
</ServiceDefinition>

ServiceConfiguration.cscfg

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration

serviceName="AzureSpendNotifier"

xmlns=http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration

osFamily="1" osVersion="*">
<Role name="GLOBAL">
<ConfigurationSettings>
<Setting name="ExampleSetting" value="Hello World" />
</ConfigurationSettings>
<Instances count="1" />
</Role>

<Role name="MyWorker">
<Instances count="1" />
<ConfigurationSettings>
</ConfigurationSettings>
</Role>
<Role name="MyWeb">
<Instances count="1" />
<ConfigurationSettings>
</ConfigurationSettings>
</Role>
</ServiceConfiguration>

It is important that all your roles contain a ConfigurationSettings entry in both cscfg and csdef files, even if it's empty- otherwise the shared configuration settings will not be inserted.

3. Open your azure deployment (.ccproj) project in notepad, and add the highlighted line below:

  ... 
<Import Project="$(CloudExtensionsDir)Microsoft.CloudService.targets" />
<Import Project="globalsettings/globalsettings.targets" />
</Project>

It is important you add this below the Microsoft.CloudService.targets import line, as it replaces some of the rules defined in that file.

Visual studio will prompt you to reload the project, say yes. At this point you will have a new Azure role called 'GLOBAL' with settings you can edit through the visual studio properties panel as normal. This role will never be deployed, but any settings you add to it will be copied to all your other roles when deployed or tested locally within visual studio.

© Simple Talk or respective owner