<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bill Lodin &#187; configuration</title>
	<atom:link href="http://blogs.itmentors.com/bill/tag/configuration/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.itmentors.com/bill</link>
	<description></description>
	<lastBuildDate>Tue, 11 Jan 2011 22:46:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SetConfigurationSettingPublisher demystified</title>
		<link>http://blogs.itmentors.com/bill/2011/01/10/setconfigurationsettingpublisher-demystified/</link>
		<comments>http://blogs.itmentors.com/bill/2011/01/10/setconfigurationsettingpublisher-demystified/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 22:52:52 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows Azure]]></category>
		<category><![CDATA[azure]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[setconfigurationsettingpublisher]]></category>

		<guid isPermaLink="false">http://blogs.itmentors.com/bill/?p=107</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>I am often asked by students about the purpose of Azure’s <em>CloudStorageAccount.SetConfigurationSettingPublisher</em> method, particularly when they experience the infamous “<em>SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used</em>” error.</p>
<p>I suppose part of it is the potentially unfamiliar syntax, so let’s start there. You will usually find <em>SetConfigurationPublisher</em> called in code that looks similar to the following:</p>
<pre>CloudStorageAccount.SetConfigurationSettingPublisher((configName,
                                                      configSettingPublisher) =&gt;
{
    string configValue = RoleEnvironment.GetConfigurationSettingValue(configName);
    configSettingPublisher(configValue);
});</pre>
<p>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.</p>
<p>First, note that the <em>SetConfigurationSettingPublisher</em> method expects a single parameter, of type <em>Action&lt;string,Func&lt;string,bool&gt;&gt;</em>. Instead of using the lambda expression, we could simply define a matching method as follows:</p>
<pre>private void MySettingPublisher(string configName,
                                Func&lt;string, bool&gt; configSettingPublisher) {
}</pre>
<p>Then the call to <em>SetConfigurationSettingPublisher</em> becomes the following:</p>
<pre>CloudStorageAccount.SetConfigurationSettingPublisher(MySettingPublisher);</pre>
<p>Now, when a call is made to retrieve settings from configuration, Azure will invoke our <em>MySettingPublisher</em> 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 <em>string</em> (called <strong>configName</strong>) and a <em>Func&lt;string,bool&gt;</em> called <strong>configSettingPublisher</strong>. 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:</p>
<pre>private void MySettingPublisher (string configName,
                                 Func&lt;string, bool&gt; configSettingPublisher)
{
   string configValue = RoleEnvironment.GetConfigurationSettingValue(configName);
   configSettingPublisher(configValue);
}</pre>
<p>When you use a method such as <em>CloudStorageAccount.FromConfigurationSetting</em>, Azure looks for a setting publisher. Thanks to an earlier call to <em>SetConfigurationSettingPublisher</em>, it finds your <em>MySettingPublisher</em> method which it dutifully invokes. Your code gets the actual value from configuration (using <em>RoleEnvironment.GetConfigurationSettingValue</em>) and invokes the method Azure provided with the <em>Func&lt;string, bool&gt;</em> parameter.</p>
<p>But why all this back-and-forth between Azure and your code? Shouldn’t <em>CloudStorageAccount.FromConfigurationSetting</em> know that you want to grab the setting out of <em>ServiceConfiguration.cscfg</em> and just take care of it for you?</p>
<p>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 <em>may or may not</em> run in Azure. No Azure, no <em>Service Configuration.cscfg</em> file so you might place the setting in the <strong>&lt;appSettings&gt;</strong> section of <em>Web.config</em>:</p>
<pre>&lt;configuration&gt;
  <strong>&lt;appSettings&gt;
    &lt;add key="MyConnectionString" value="UseDevelopmentStorage=true" /&gt;
  &lt;/appSettings&gt;
</strong>&lt;/configuration&gt;</pre>
<p>Now, let’s modify our <em>MySettingPublisher</em> method as follows:</p>
<pre>private void MySettingPublisher(string configName,
                                Func&lt;string, bool&gt; configSettingPublisher)
{
  var configValue = RoleEnvironment.IsAvailable ?
       RoleEnvironment.GetConfigurationSettingValue(configName) :
       System.Web.Configuration.WebConfigurationManager.AppSettings[configName];
  configSettingPublisher(configValue);</pre>
<pre>}</pre>
<p>With this in place we can run our Web application outside Azure and it still works, it just uses <em>Web.config</em> instead of <em>ServiceConfiguration.cscfg</em>.</p>
<p>By the way, as Steve Marx <a title="How to Resolve “SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used” After Moving to Windows Azure SDK 1.3" href="http://blog.smarx.com/posts/how-to-resolve-setconfigurationsettingpublisher-needs-to-be-called-before-fromconfigurationsetting-can-be-used-after-moving-to-windows-azure-sdk-1-3">points out</a>, 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:</p>
<pre>string configValue = RoleEnvironment.GetConfigurationSettingValue("MyConnectionString");
CloudStorageAccount account = CloudStorageAccount.Parse(configValue);</pre>
<p> </p>
<p>Sample code <a title="Azure configuration sample code" href="http://www.itmentors.com/code/2011/01/AzureConfigDemo.zip">here</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.itmentors.com/bill/2011/01/10/setconfigurationsettingpublisher-demystified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configuration Files and Windows Azure</title>
		<link>http://blogs.itmentors.com/bill/2009/11/04/configuration-files-and-windows-azure/</link>
		<comments>http://blogs.itmentors.com/bill/2009/11/04/configuration-files-and-windows-azure/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 19:15:05 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows Azure]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[ServiceConfiguration.cscfg]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://blogs.itmentors.com/bill/?p=58</guid>
		<description><![CDATA[Last week I posted a short article on how easy it is to move an ASP.NET Web Application to the cloud&#8230; and it is extremely easy, as long as you don&#8217;t have any data, configuration settings, 3rd party libraries, or other issues getting in the way! Actually, all kidding aside, it is pretty easy as [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I posted a short article on how easy it is to move an ASP.NET Web Application to the cloud&#8230; and it is extremely easy, as long as you don&#8217;t have any data, configuration settings, 3rd party libraries, or other issues getting in the way! Actually, all kidding aside, it is pretty easy as long as you understand what’s different and what’s similar when you’re running in the cloud. With a little effort, you should be able to come up with a strategy that doesn’t force you to re-engineer your application; you might even be able to keep your Web application in a state that can be deployed to the Web or to the cloud without any configuration changes!</p>
<p>In this post I’d like to address <strong>configuration</strong>. If you’re an ASP.NET Web developer, you already know all about <em>web.config</em>, and you probably use it all the time to store configuration settings for your application. Let’s assume you want to store a simple message in configuration; your <em>web.config</em> file would contain the following:</p>
<pre>&lt;appSettings&gt;
   &lt;add key="messageText" value="Hello, ASP.NET!"/&gt;
&lt;/appSettings&gt;</pre>
<p>Then you could write some application code that retrieves the value (in this case, displaying it on the Web page using an ASP.NET Label control):</p>
<pre>message.Text = WebConfigurationManager.AppSettings["messageText"];</pre>
<p>The good news is that Web applications running in Windows Azure support <em>web.config</em> so this code will work without any modifications whatsoever. The bad news is that when you deploy an Azure application, the <em>web.config</em> file gets packaged up along with the rest of the Web application components in a <em>.csx</em> file. This sort of defeats the purpose of putting your message in a configuration file, yes? If you didn’t need the ability to change the message without recompiling your application you could have just placed the message text directly into your code. So <em>web.config</em> is a great place for application settings unless you’re app is in the cloud&#8230;</p>
<p>Ah, but Windows Azure does support the idea of a configuration file – it goes by the name <em>ServiceConfiguration.cscfg</em> and it gets deployed separately from the <em>.csx</em> package. And, you can add your own custom settings to it, just like <em>web.config</em>. It takes a bit more effort, since you must first define the setting in <em>ServiceDefinition.csdef</em>:</p>
<pre>&lt;ConfigurationSettings&gt;
   &lt;Setting name="messageText"/&gt;
&lt;/ConfigurationSettings&gt;</pre>
<p>…and then set the actual value in <em>ServiceConfiguration.cscfg</em>:</p>
<pre>&lt;ConfigurationSettings&gt;
   &lt;Setting name="messageText" value="Hello, Azure!"/&gt;
&lt;/ConfigurationSettings&gt;</pre>
<p>Once you’ve done that, you can retrieve the value quite easily:</p>
<pre>message.Text = RoleManager.GetConfigurationSetting("messageText");</pre>
<p>If you’re moving your application into the cloud and you’re “not looking back,” it may make sense to move your settings into the <em>.cscfg</em> file and use that exclusive of <em>web.config</em>. But what about building an application that can be deployed in AND out of the cloud?</p>
<p>Fortunately, it is not difficult to determine at runtime whether you are running in or out of the cloud and retrieve configuration settings as appropriate. In the following code, note how we can check the <em>RoleManager.IsRoleManagerRunning</em> property:</p>
<pre>if (RoleManager.IsRoleManagerRunning)
{
   // we're in the cloud
   message.Text = RoleManager.GetConfigurationSetting("messageText");
}
else if (System.Web.HttpContext.Current != null)
{
   // we're NOT in the cloud, but we're still in a Web application
   message.Text = WebConfigurationManager.AppSettings["messageText"];
}
else
{
   // not in the cloud, not in the web... desktop app maybe?
   message.Text = ConfigurationManager.AppSettings["messageText"];
}</pre>
<p>As you can see, we’ll get the value stored in <em>.cscfg</em> if we’re running in the fabric (the cloud), we get the one from <em>web.config</em> if we’re a “regular” ASP.NET Web application and we can even fall back to the standard .NET Configuration manager.</p>
<p>One more thing – checking <em>RoleManager.IsRoleManagerRunning</em> all the time will surely get tedious if you have a lot of settings to retrieve. You may want to encapsulate the settings retrieval into a reusable component, perhaps similar to the following:</p>
<pre>public class SettingsManager
{
   private static Settings _settings = new Settings();
   public static Settings Settings
   {
      get
      {
         return _settings;
      }
   }
}</pre>
<pre>public class Settings
{
   public string this[string key]
   {
      get
      {
         if (RoleManager.IsRoleManagerRunning)
         {
            return RoleManager.GetConfigurationSetting(key);
         }
         else if (System.Web.HttpContext.Current != null)
         {
            return WebConfigurationManager.AppSettings[key];
         }
         return string.Empty;
      }
   }
}</pre>
<p>You can find a much more comprehensive example (one that also deals with logging) in the <strong>HelloFabric</strong> sample that ships with the <a title="Windows Azure SDK" href="http://go.microsoft.com/fwlink/?LinkID=130232">Windows Azure SDK</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.itmentors.com/bill/2009/11/04/configuration-files-and-windows-azure/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

