<?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; ServiceConfiguration.cscfg</title>
	<atom:link href="http://blogs.itmentors.com/bill/tag/serviceconfiguration-cscfg/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>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>

