SetConfigurationSettingPublisher() – Azure Storage Client v1.0

In my first post on Azure Storage Client v1.0 (download) I wrote the following:

The solution to this is to invoke the SetConfigurationSettingPublisher() method in the (new) OnStart() method for the role. The code required to do this is sufficiently gnarly that the simplest technique appears to be to copy it directly from the thumbnails sample provided with the Azure SDK v1.0.

After looking into SetConfigurationSettingPublisher() a bit more I haven’t really changed my mind about it being gnarly. Indeed, I think the idea behind SetConfigurationSettingPublisher() is far too clever for its own good. I am probably a bit forward in my criticism given that it is still barely documented.

My initial problem comes from its declaration in the CloudStorageAccount class:

public static void SetConfigurationSettingPublisher (
    Action<string,Func<string,bool>> configurationSettingPublisher

I confess to not being a frequent user of delegates, but I think a declaration that wraps one delegate, Func(T1, T2), inside another, Action(T1,T2), is asking for trouble. For the benefit of those like myself not overly familiar with them the declarations of the Action(T1,T2) and Func(T1,T2) delegates used here are:

public delegate void Action<T1, T2>( T1 arg1, T2 arg2 )public delegate TResult Func<T, TResult>( T arg )

All this just seems a little too clever for me. It is not always the case that extremely elegant solutions are necessarily the best solutions – and I think this is one of these cases. Part of the problem lies, of course, in the lack of documentation – which time will rectify.

The documentation for SetConfigurationSettingPublisher() states:

A configuration setting publisher allows adding subscribers for particular configuration settings.This method should be called once to set up the environment. The environment could be the Windows Azure runtime, in which case the publisher is a simple wrapper of the configuration reading and change event. The environment could also be a .NET environment, in which case the developer can hook up a custom configuration reader and change notification. (my emphasis)

The idea is that SetConfigurationSettingPublisher() provides an extension point where different configuration providers can be hooked in so that their contents can be accessed and changes to them identified. So far I have seen only two examples.

The thumbnails sample from the Azure SDK v1.0 uses (in WebRole.cs):

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>

In an Azure Forum thread, Fernando Tubio suggests for a console app (or presumably any app that can use the ConfigurationManager to access its configuration)

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>

The idea appears to be that the call to SetConfigurationSettingPublisher() stores the configurationSettingPublisher Action(T1,T2) in CloudStorageAccount. The Action is invoked when  CloudStorageAccount.FromConfigurationSetting() method is called to create a CloudStorageAccount object. FromConfigurationSetting() is declared:

public static CloudStorageAccount FromConfigurationSetting ( String settingName )

Calling FromConfigurationSetting() invokes the configurationSettingPublisher Action(T1,T2) with configName set to settingName and configSetter set to a private CloudStorageAccount method that initializes the CloudStorageAccount object from the specified setting in the configuration.

For example, the following initializes a newly created CloudStorageAccount object from the DataConnectionString setting in the Azure configuration.

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);

The documentation for SetConfigurationSettingPublisher() suggests it is also used for change notification. The thumbnails sample shows this:

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
        configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));        RoleEnvironment.Changed += (sender, arg) =>
            if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any((change) => (change.ConfigurationSettingName == configName)))
                if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))

The RoleEnvironment class in the Microsoft.WindowsAzure.ServiceRuntime namespace represents the “Windows Azure environment in which an instance of a role is running.” Its Changed event “occurs after a change to the service configuration has been applied to the running instances of the role.” In the thumbnails example this is used to invoke RoleEnvironment.RequestRecycle() which requests that the current role is stopped and restarted. I’m not sure if this piece of code is not in thumbnails purely for demonstration because my understanding was that any change to the Azure configuration automatically caused the role to be stopped and restarted using upgrade domains. I wonder if this functionality is not there to support some as yet unannounced functionality.

UPDATE 3/8/2010

Steven Nagy has a good post going into greater depth on the use of configuration settings.

UPDATE 5/6/2010

If you do not need the functionality provided by SetConfigurationSettingPublisher() you can get a CloudStorageAccount object using CloudStorageAccount.Parse() as follows:

CloudStorageAccount cloudStorageAccount =
    CloudStorageAccount.Parse( RoleEnvironment.GetConfigurationSettingValue(“DataConnectionString”));

UPDATE 12/5/2010
Prior to Azure SDK v1.3 web roles used hosted web core not full IIS. With Azure SDK v1.3 it is now possible to use full IIS. This causes a significant difference in where in code SetConfigurationSettingPublisher() is invoked. This Azure Team Blog post describes this change in more detail.

With Azure SDK v1.3 it is now possible to use full IIS in webroles as well as the hosted web core previousl


About Neil Mackenzie

Cloud Solutions Architect. Microsoft
This entry was posted in Storage Service, Windows Azure. Bookmark the permalink.

5 Responses to SetConfigurationSettingPublisher() – Azure Storage Client v1.0

  1. Aleks says:

    Neil, There are few reasons for this functionality:1) Remove any dependency of StorageClient on the runtime to allow usage of storage client in variety of non-cloud scenarios. Having to install the SDK just to be able to use a cloud storage because of the convenience method seemed inconvenient.2) Allow for automatic key rotation just by a change of configuration without role restat. (NOTE: Windows Azure will not restart your role if you don’t ask with the Nov’09 SDK. The default template ‘cancels’ the RoleEnvironmentChanging event which causes a restart.)3) Allow for different ways of delivering the configuration (such as web.config updates in non-Azure environments)

  2. Neil says:

    Thanks for the clarification, HerrShaft.I know there has been a lot of interest in using Storage Client – or at least the underlying storage mechanism – outside Azure, so presumably 1) and 3) go som ways towards meeting that demand.2) is also interesting in that it is a simple, yet large, change to the way configuration changes have been handled so far. Presumably, all will become clear as the documentation on the November release comes out.

  3. Dexter says:

    I think my brain is officially hemorrhaging from trying to understand this. The lack of documentation of this feature just makes it worse.

  4. Dexter says:

    OK, i finally understood what this is all about, thanks to this post ( the Azure Forum thread post by Tubio essentially said the same thing in the last paragraph, I think. I just want to load my Azure settings dynamically from a config file…Microsoft is making it complicated for my tiny brain. Of course, nothing against you, Neil… 🙂

  5. Pingback: Yet another application to handle Windows Azure Storage Services - Paolo Salvatori's Blog - Site Home - MSDN Blogs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s