Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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.</p> <p>Here is a brief console app example (DevModeDependencyTest):</p> <p>App.config :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;sectionGroup name="DevModeSettings"&gt; &lt;section name="debug" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" /&gt; &lt;section name="release" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" /&gt; &lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;DevModeSettings&gt; &lt;debug webServiceUrl="http://myDebuggableWebService.MyURL.com" /&gt; &lt;release webServiceUrl="http://myWebservice.MyURL.com" /&gt; &lt;/DevModeSettings&gt; &lt;appSettings&gt; &lt;add key="webServiceUrl" value="http://myWebservice.MyURL.com" /&gt; &lt;/appSettings&gt; &lt;/configuration&gt; </code></pre> <p>The object to store your custom configuration (DevModeSettings.cs):</p> <pre><code> 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; } } } } </code></pre> <p>A handler to access your custom configuration settings (DevModeSettingsHandler.cs) :</p> <pre><code> 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; } } } </code></pre> <p>And finally your entry point to the console app (DevModeDependencyTest.cs) :</p> <pre><code> 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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload