Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Client Configurations for Different Mobile Service Environments

In my previous post I talked about setting up different instance of the backend cloud services. The next thing is to control which environment a given build of the client applications will point to. I used to do this with build configurations (eg defining compilation symbols like DEBUG and TEST) to toggle which Constants are compiled into the application. This is a little painful if you want to actually debug against production (ie you want to run the application pointing at production data but in debug mode so you can step through code). It also meant that my configuration information quickly became distributed all over the place in my Constants file. I’m going to include here a simple Configuration class that I’m now using as an alternative. Note that I actually still use compilation constants as the default way of specifying which build configuration is used. However it can easily be overridden to allow for debugging against test or production.

public class Configuration
{
    static Configuration()
    {
#pragma warning disable 162 // This is to allow for easy override of configuration values to debug issues
        if (false)
        {
            Debug.WriteLine("-----------------WARNING - Default Configuration Values Overridden ------------------");
            Current = Configurations[ConfigurationType.Production];
        }
#pragma warning restore 162

#if DEBUG
#if DEBUGLOCAL
        Current = Configurations[ConfigurationType.LocalDevelopment];
#else
        Current = Configurations[ConfigurationType.Development];
#endif
#elif TEST
        Current = Configurations[ConfigurationType.LocalDevelopment];
#else
        Current = Configurations[ConfigurationType.Production];
#endif
    }

    public static Configuration Current { get; set; }

    public string ADTenant { get; set; }

    public string ADAuthority
    {
        get { return ClientConstants.ADAuthorityRoot + ADTenant; }
    }

    public string ADNativeClientApplicationClientId { get; set; }

    public string ADRedirectUri { get; set; }

    public string MobileServiceRootUri { get; set; }
    public string MobileServiceAppIdUri { get; set; }

    public string MobileServiceApiKey { get; set; }

    public enum ConfigurationType
    {
        LocalDevelopment,
        Development,
        Test,
        Production
    }

    public static IDictionary<ConfigurationType, Configuration> Configurations
    {
        get { return configurations; }
    }

    private static readonly IDictionary<ConfigurationType, Configuration> configurations
        = new Dictionary<ConfigurationType, Configuration>
        {
            {
                ConfigurationType.LocalDevelopment, new Configuration
                {
                    ADTenant = "realestateinspector.onmicrosoft.com",
                    ADNativeClientApplicationClientId = "a5a10ee9-zzzzzzz-4bde-997f-3f1c323fefa5",
                    ADRedirectUri = "
http://builttoroam.com",
                    MobileServiceRootUri = "
http://localhost:51539/",
                    MobileServiceAppIdUri =
https://realestateinspectordev.azure-mobile.net/login/aad,
                    MobileServiceApiKey="wpxaI---------------------------EBcg12"
                }
            },
            {
                ConfigurationType.Development, new Configuration
                {
                    ADTenant = "realestateinspector.onmicrosoft.com",
                    ADNativeClientApplicationClientId = "a5a10ee9-zzzzzzz-4bde-997f-3f1c323fefa5",
                    ADRedirectUri = "
http://builttoroam.com",
                    MobileServiceRootUri =
https://realestateinspectordev.azure-mobile.net/,
                    MobileServiceAppIdUri =
https://realestateinspectordev.azure-mobile.net/login/aad,
                    MobileServiceApiKey="wpxaI---------------------------EBcg12"
                }
            },
            {
                ConfigurationType.Test, new Configuration
                {
                    ADTenant = "realestateinspector.onmicrosoft.com",
                    ADNativeClientApplicationClientId = "a5a10ee9-tttt-4bde-997f-3f1c323fefa5",
                    ADRedirectUri = "
http://builttoroam.com",
                    MobileServiceRootUri =
https://realestateinspectortest.azure-mobile.net/,
                    MobileServiceAppIdUri =
https://realestateinspectortest.azure-mobile.net/login/aad,
                    MobileServiceApiKey="wpxaI---------------------------EBcg12"
                }
            },
            {
                ConfigurationType.Production, new Configuration
                {
                    ADTenant = "realestateinspector.onmicrosoft.com",
                    ADNativeClientApplicationClientId = "a5a10ee9-wwww-4bde-997f-3f1c323fefa5",
                    ADRedirectUri = "
http://builttoroam.com",
                    MobileServiceRootUri = "
https://realestateinspector.azure-mobile.net/",
                    MobileServiceAppIdUri = "
https://realestateinspector.azure-mobile.net/login/aad",
                    MobileServiceApiKey="wpxaI---------------------------EBcg12"
                }
            }
        };
}

Adding a new configuration is easy:
- define a new enumeration value (eg LocalDevelopment)
- create a new entry in the Configurations dictionary
- (optional) test by changing “if (false)” to “if (true)” and changing the specified configuration

Pingbacks and trackbacks (1)+

Comments are closed