views:

50

answers:

2

Is anyone aware of a way that I can set application (or user) level settings in a .Net application that are conditional on the applications current development mode? IE: Debug/Release

To be more specific, I have a url reference to my webservices held in my application settings. During release mode I would like those settings to point to http://myWebservice.MyURL.com during debug mode I would love those settings to be http://myDebuggableWebService.MyURL.com.

Any ideas?

+1  A: 

There is, afaik, no builtin way of doing this. In our project we maintain 4 different settings files, and switch between them by copying each into the active file in the prebuild step of the build.

copy "$(ProjectDir)properties\settings.settings.$(ConfigurationName).xml" "$(ProjectDir)properties\settings.settings"
copy "$(ProjectDir)properties\settings.designer.$(ConfigurationName).cs" "$(ProjectDir)properties\settings.Designer.cs"

This has worked flawlessly for us for a few years. Simply change the target and the entire config file is switched aswell.

edit: The files are named e.g. settings.settings.Debug.xml, settings.settings.Release.xml etc..

Scott Hanselman has described a slightly 'smarter' approach, only diff is that we don't have the check to see if the file has changed.

http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

hhravn
+1  A: 

If you want to keep everything in one configuration file you can introduce a custom configuration section to your app.settings to store properties for debug and release modes.

You can either persist the object in your app that stores dev mode specific settings or override an existing appsetting based on the debug switch.

Here is a brief console app example (DevModeDependencyTest):

App.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="DevModeSettings">
      <section name="debug" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
      <section name="release" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>
  <DevModeSettings>
    <debug webServiceUrl="http://myDebuggableWebService.MyURL.com" />
    <release webServiceUrl="http://myWebservice.MyURL.com" />
  </DevModeSettings>
  <appSettings>
    <add key="webServiceUrl" value="http://myWebservice.MyURL.com" />
  </appSettings>
</configuration>

The object to store your custom configuration (DevModeSettings.cs):

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DevModeDependencyTest
{
    public class DevModeSetting : ConfigurationSection
    {
        public override bool IsReadOnly()
        {
            return false;
        }

        [ConfigurationProperty("webServiceUrl", IsRequired = false)]
        public string WebServiceUrl
        {
            get
            {
                return (string)this["webServiceUrl"];
            }
            set
            {
                this["webServiceUrl"] = value;
            }
        }
    }
}

A handler to access your custom configuration settings (DevModeSettingsHandler.cs) :

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DevModeDependencyTest
{
    public class DevModeSettingsHandler
    {
        public static DevModeSetting GetDevModeSetting()
        {
            return GetDevModeSetting("debug");
        }

        public static DevModeSetting GetDevModeSetting(string devMode)
        {
            string section = "DevModeSettings/" + devMode;

            ConfigurationManager.RefreshSection(section); // This must be done to flush out previous overrides
            DevModeSetting config = (DevModeSetting)ConfigurationManager.GetSection(section);

            if (config != null)
            {
                // Perform validation etc...
            }
            else
            {
                throw new ConfigurationErrorsException("oops!");
            }

            return config;
        }
    }
}

And finally your entry point to the console app (DevModeDependencyTest.cs) :

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DevModeDependencyTest
{
    class DevModeDependencyTest
    {
        static void Main(string[] args)
        {
            DevModeSetting devMode = new DevModeSetting();

            #if (DEBUG)
                devMode = DevModeSettingsHandler.GetDevModeSetting("debug");
                ConfigurationManager.AppSettings["webServiceUrl"] = devMode.WebServiceUrl;
            #endif

            Console.WriteLine(ConfigurationManager.AppSettings["webServiceUrl"]);
            Console.ReadLine();
        }
    }
}
jadusty