SetConfigurationSettingPublisher demystified
Posted by bill in Microsoft, Windows Azure on January 10, 2011
I am often asked by students about the purpose of Azure’s CloudStorageAccount.SetConfigurationSettingPublisher method, particularly when they experience the infamous “SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used” error.
I suppose part of it is the potentially unfamiliar syntax, so let’s start there. You will usually find SetConfigurationPublisher called in code that looks similar to the following:
CloudStorageAccount.SetConfigurationSettingPublisher((configName,
configSettingPublisher) =>
{
string configValue = RoleEnvironment.GetConfigurationSettingValue(configName);
configSettingPublisher(configValue);
});
In a nutshell, you’re invoking a method that expects a method that takes a method as a parameter. Confused? No worries – it becomes clearer once you unravel the lambda expression.
First, note that the SetConfigurationSettingPublisher method expects a single parameter, of type Action<string,Func<string,bool>>. Instead of using the lambda expression, we could simply define a matching method as follows:
private void MySettingPublisher(string configName,
Func<string, bool> configSettingPublisher) {
}
Then the call to SetConfigurationSettingPublisher becomes the following:
CloudStorageAccount.SetConfigurationSettingPublisher(MySettingPublisher);
Now, when a call is made to retrieve settings from configuration, Azure will invoke our MySettingPublisher method. So far so good, but it won’t work yet because our method doesn’t actually do anything. If you look at the parameters that are passed to our method, you will notice that we receive a string (called configName) and a Func<string,bool> called configSettingPublisher. In other words, Azure will give us the name of the configuration setting that is being retrieved as well as a method that expects a string and returns a Boolean. We just need to call that method once we’ve figured out what the value for the setting should be. So, a few lines of implementation are all it takes to complete our code:
private void MySettingPublisher (string configName,
Func<string, bool> configSettingPublisher)
{
string configValue = RoleEnvironment.GetConfigurationSettingValue(configName);
configSettingPublisher(configValue);
}
When you use a method such as CloudStorageAccount.FromConfigurationSetting, Azure looks for a setting publisher. Thanks to an earlier call to SetConfigurationSettingPublisher, it finds your MySettingPublisher method which it dutifully invokes. Your code gets the actual value from configuration (using RoleEnvironment.GetConfigurationSettingValue) and invokes the method Azure provided with the Func<string, bool> parameter.
But why all this back-and-forth between Azure and your code? Shouldn’t CloudStorageAccount.FromConfigurationSetting know that you want to grab the setting out of ServiceConfiguration.cscfg and just take care of it for you?
It turns out that being able to set your own “setting publisher” has code reusability benefits. The most likely scenario is building a Web application that may or may not run in Azure. No Azure, no Service Configuration.cscfg file so you might place the setting in the <appSettings> section of Web.config:
<configuration> <appSettings> <add key="MyConnectionString" value="UseDevelopmentStorage=true" /> </appSettings> </configuration>
Now, let’s modify our MySettingPublisher method as follows:
private void MySettingPublisher(string configName,
Func<string, bool> configSettingPublisher)
{
var configValue = RoleEnvironment.IsAvailable ?
RoleEnvironment.GetConfigurationSettingValue(configName) :
System.Web.Configuration.WebConfigurationManager.AppSettings[configName];
configSettingPublisher(configValue);
}
With this in place we can run our Web application outside Azure and it still works, it just uses Web.config instead of ServiceConfiguration.cscfg.
By the way, as Steve Marx points out, if you are building an Azure-specific solution and would rather not worry about all this “setting publisher” business, you could simply use the following to initialize your storage account:
string configValue = RoleEnvironment.GetConfigurationSettingValue("MyConnectionString");
CloudStorageAccount account = CloudStorageAccount.Parse(configValue);
Sample code here!
More Silverlight 4 Web seminars
Posted by bill in Microsoft, Silverlight on June 9, 2010
Four more Silverlight 4 videos up! Here are the links and source code:
- Silverlight 4: Printing in Silverlight [code]
- Silverlight 4: Webcam and Microphone Support in Silverlight [code]
- Silverlight 4: Silverlight and the Managed Extensibility Framework [code]
- Silverlight 4: Silverlight and WCF RIA Services [code]
UPDATE: Just added the final two videos in the series!
What’s New in Silverlight 4
Posted by bill in Microsoft, Silverlight, Visual Studio on May 3, 2010
Just started a series for MSDev about the new features in Silverlight 4. Two videos were posted this morning – here are the direct links along with links to source code.
Silverlight 4: The RichTextBox Control [code]
Silverlight 4: The Viewbox Control [code]
Look for more videos in the coming weeks!
New Windows Phone videos now up on MSDev
Posted by bill in Microsoft, Windows Phone on April 19, 2010
It’s been a busy couple of months, with travel to Holland, the UK, Las Vegas and Seattle. In-between the travel I’ve been posting some new sessions on Windows Phone 7. It was great to see all the news about the Windows Phone development platform at MIX2010, and I hope you enjoy the first five “Windows Phone 7 in 7″ videos I’ve posted over at MSDev.
You can find the series here… Direct links (including the source code for the XNA video):
Learn Windows 7, 7 minutes at a time!
Posted by bill in Microsoft, Visual Studio on February 8, 2010
Nancy and I are working on a series of short videos for msdev – each one focuses on developing for Windows 7 and each one is less than 7 minutes long! I just posted the first six of mine this morning – here’s the list (with links to source code!)
- Windows 7 Taskbar “Thumb Buttons” [code]
- Windows 7 Taskbar Icon Overlays [code]
- Windows 7 Taskbar Jump Lists [code]
- Windows 7 Taskbar “Hot Colors” [code]
- Windows 7 Touch [code]
- Windows 7 Multitouch [code]
Enjoy!
UPDATE: More videos just posted!
- Programming with Inertia in Windows 7 [code]
- “Manipulation” in WPF and Windows 7 [code]
- Windows 7 Gestures [code]
- Windows 7 and Ink [code]
- Handwriting Recognition in Windows 7 [code]
- Handwritten Math Recognition in Windows 7 [code]
Regarding the last video (math recognition), credit for the “MathML to C#” application belongs to Dmitri Nesteruk. You can find his “mmlsharp” application at Google Code (although I downloaded the source from CodePlex). You can also read his original article about the application.
Revisiting the Windows Azure “How Do I” videos
Posted by bill in Microsoft, Windows Azure on January 21, 2010
About a year ago I started posting “How Do I” videos on topics related to the Windows Azure Platform (of course, back then it was known as the “Azure Services Platform” and there were quite a few differences.) Needless to say, every one of these videos is now out-of-date because of the changes and improvements that Microsoft has made during a year of CTP testing.
I’ll be updating a number of these videos, starting with the following which have recently been posted to the msdev site:
- Windows Azure How To Guide: Getting Started Developing on Windows Azure
- Windows Azure How To Guide: Storing Blobs in Windows Azure Storage
The video on BlobStorage includes a demonstration app – you can download the source code here.
Cheers!
More WPF 4 Web Seminars
Posted by bill in Microsoft, Visual Studio, WPF on January 18, 2010
Greetings from rainy Southern California! Just posted three new Web seminars on Windows Presentation Foundation. These continue the WPF 4 series I’ve been working on, and two of them have accompanying source code:
- Binding in Windows Presentation Foundation 4 [code]
- Windows Presentation Foundation and Windows 7 [code]
- The Windows Presentation Foundation Designer
Enjoy!
UPDATE: Just posted the final “WPF 4″ video in the series, “XAML Browser Applications”. Source code here.
Windows Mobile 6.5 Development
Posted by bill in Microsoft, Uncategorized, Visual Studio, Windows Azure on January 11, 2010
Just posted a 30-minute session on Windows Mobile 6.5 – if you’re new to Windows Mobile development and you want to get up to speed quickly, this is a good place to start! Here’s the link to the session:
http://www.msdev.com/Directory/Description.aspx?eventId=1665
This is actually the first in an eight-part series on WM6.5 that IT Mentors is doing for msdev. Oh, and you can download the demo that I used here (note that in order for this demo to work you’ll need to grab the managed code wrapper for Windows Mobile Gestures!)
WPF 4 Web Seminars
Posted by bill in Microsoft, Visual Studio, WPF on January 11, 2010
Just posted the first four Web seminars in a new series about Windows Presentation Foundation 4! Each session is short (10-20 minutes) and focuses a feature or two of WPF 4. Here are the links to the individual sessions, each with source code you can download if you’d like:
- New Windows Presentation Foundation Controls [code]
- The Visual State Manager[code]
- Touch comes to Windows Presentation Foundation[code]
- Graphics Enhancements[code]
Cheers!
New AppFabric Web Seminars
Posted by bill in Microsoft, Windows Azure on December 14, 2009
This morning we posted four new videos as part of the “Everything You Need to Know About Azure as a Developer” series. These focus on the Windows Azure platform AppFabric:
Source code for the “AppFabric Fundamentals” session can be downloaded here. The source code I used in the Service Bus and ACS sessions is from the AppFabric SDK, which you can get here.
Cheers!