2

We will use Windows Azure Websites for our basic hosting needs in the future. And since there are very specific configuration settings for cloud vs local how do you manage these settings? Take for example the following:

Local Development Server:

string path = AppSettings["StoragePath"];

<add key="StoragePath" value="c:\temp" />

Windows Azure:

string path = AppSettings["StoragePath"];

<add key="StoragePath" value="xyz" />

Do you manually change the StoragePath in the config file before each release OR is there something in code that can be done such as:

<add key="LocalStoragePath" value="c:\temp" />
<add key="BlobStoragePath" value="xyz" />    

string path;
if (Azure)
{
   path = AppSettings["BlobStoragePath"];
}
else
{
   path = AppSettings["LocalStoragePath"];
}

If the later is possible, how can i determine if the environment is Windows Azure?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
atp03
  • 3,539
  • 3
  • 17
  • 20
  • possible duplicate of [Determine if app is running in azure or not](http://stackoverflow.com/questions/873901/determine-if-app-is-running-in-azure-or-not) – Mitch Wheat Jan 28 '13 at 04:03
  • why cant you use the serviceconfiguration.local and serviceconfiguration.cloud's configurationsettings? – allen Jan 28 '13 at 04:29
  • @allen I'm open to that, just that i wasn't aware of it. Does serviceconfiguration replace AppSettings completely? I should also add that we're deploying simple websites and not VMs, if that makes a difference. – atp03 Jan 28 '13 at 04:50

3 Answers3

1

I typically create a new build configuration (called Azure).

Then in the web.config create your keys..

<add key="LocalStoragePath" value="c:\blah" />
<add key="AzureStoragePath" value="xyz" />

in your code write:

#if CONFIG == "Azure"
  public const String storageKey = "AzureStoragePath";
#endif CONFIG == "Debug"
  public const String storageKey = "LocalStoragePath";
#endif

And use it:

String path = AppSettings[storageKey];
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
1
public interface IConfigurationProvider { }

public class AzureConfigurationProvider : IConfigurationProvider { }

public class LocalConfigurationProvider : IConfigurationProvider { }

public static class ConfigurationProviderFactory
{
    private static bool _isAzure = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable;

    private static Lazy<IConfigurationProvider> _provider = Lazy<IConfigurationProvider>(GetProvider);

    private static IConfigurationProvider GetProvider()
    {
         return _isAzure ?
             new AzureConfigurationProvider() :
             new LocalConfigurationProvider();
    }

    public static IConfigurationProvider Instance
    {
        get { return _provider.Value; }
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

Assuming you're using the latest version of Web Publishing feature in VS 2010 or VS2012 you can accomplish this fairly easily with your publish profile and a web.config transform.

First, create your publish profile (right-click the project, select Publish, go through the dialog). This will be the default place to make a variety of config change anyways, such as connection strings.

Then, right-click the the .pubxml file created for your publish profile and there should be an option to add a transform. This will add a new web..config, which should appear next to the web.Debug.config/web.Release.config.

In that file you can add a transform for the app setting you want to change. The transformation value will be applied when you publish using that profile; local development will still use whatever value you want.

Jimmy
  • 27,142
  • 5
  • 87
  • 100