The Labs \ Source Viewer \ SSCLI \ System.Configuration \ ConfigurationManager

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConfigurationManager.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.    
  18.     using System.Collections;
  19.     using System.Collections.Specialized;
  20.     using System.Globalization;
  21.     using System.Configuration.Internal;
  22.     using System.IO;
  23.     using System.Security.Permissions;
  24.     using System.Threading;
  25.    
  26.     public static class ConfigurationManager
  27.     {
  28.         // The Configuration System
  29.         private static IInternalConfigSystem s_configSystem;
  30.        
  31.         // Initialization state
  32.         private static InitState s_initState;
  33.         private static object s_initLock;
  34.         private static Exception s_initError;
  35.        
  36.         private enum InitState
  37.         {
  38.             // Initialization has not yet started.
  39.             NotStarted = 0,
  40.            
  41.             // Initialization has started.
  42.             Started,
  43.            
  44.             // The config system can be used, but initialization is not yet complete.
  45.             Usable,
  46.            
  47.             // The config system has been completely initialized.
  48.             Completed
  49.         }
  50.        
  51.         static ConfigurationManager()
  52.         {
  53.             s_initState = InitState.NotStarted;
  54.             s_initLock = new object();
  55.         }
  56.        
  57.         // to be used by System.Diagnostics to avoid false config results during config init
  58.         static internal bool SetConfigurationSystemInProgress {
  59.             get { return InitState.NotStarted < s_initState && s_initState < InitState.Completed; }
  60.         }
  61.        
  62.         // Called by ASP.NET to allow hierarchical configuration settings and ASP.NET specific extenstions.
  63.         static internal void SetConfigurationSystem(IInternalConfigSystem configSystem, bool initComplete)
  64.         {
  65.             lock (s_initLock) {
  66.                 // It is an error if the configuration system has already been set.
  67.                 if (s_initState != InitState.NotStarted) {
  68.                     throw new InvalidOperationException(SR.GetString(SR.Config_system_already_set));
  69.                 }
  70.                
  71.                 s_configSystem = configSystem;
  72.                 if (initComplete) {
  73.                     s_initState = InitState.Completed;
  74.                 }
  75.                 else {
  76.                     s_initState = InitState.Usable;
  77.                 }
  78.             }
  79.         }
  80.        
  81.         private static void EnsureConfigurationSystem()
  82.         {
  83.             // If a configuration system has not yet been set,
  84.             // create the DefaultConfigurationSystem for exe's.
  85.             lock (s_initLock) {
  86.                 if (s_initState < InitState.Usable) {
  87.                     s_initState = InitState.Started;
  88.                     try {
  89.                         try {
  90.                             // Create the system, but let it initialize itself
  91.                             // when GetConfig is called, so that it can handle its
  92.                             // own re-entrancy issues during initialization.
  93.                             // When initialization is complete, the DefaultConfigurationSystem
  94.                             // will call CompleteConfigInit to mark initialization as
  95.                             // having completed.
  96.                             // Note: the ClientConfigurationSystem has a 2-stage initialization,
  97.                             // and that's why s_initState isn't set to InitState.Completed yet.
  98.                             s_configSystem = new ClientConfigurationSystem();
  99.                             s_initState = InitState.Usable;
  100.                         }
  101.                         catch (Exception e) {
  102.                             s_initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error), e);
  103.                             throw s_initError;
  104.                         }
  105.                         catch {
  106.                             s_initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error));
  107.                             throw s_initError;
  108.                         }
  109.                     }
  110.                     catch {
  111.                         s_initState = InitState.Completed;
  112.                         throw;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.        
  118.         // Set the initialization error.
  119.         static internal void SetInitError(Exception initError)
  120.         {
  121.             s_initError = initError;
  122.         }
  123.        
  124.         // Mark intiailization as having completed.
  125.         static internal void CompleteConfigInit()
  126.         {
  127.             lock (s_initLock) {
  128.                 s_initState = InitState.Completed;
  129.             }
  130.         }
  131.        
  132.        
  133.         private static void PrepareConfigSystem()
  134.         {
  135.             // Ensure the configuration system is usable.
  136.             if (s_initState < InitState.Usable) {
  137.                 EnsureConfigurationSystem();
  138.             }
  139.            
  140.             // If there was an initialization error, throw it.
  141.             if (s_initError != null) {
  142.                 throw s_initError;
  143.             }
  144.         }
  145.        
  146.         static internal bool SupportsUserConfig {
  147.             get {
  148.                 PrepareConfigSystem();
  149.                
  150.                 return s_configSystem.SupportsUserConfig;
  151.             }
  152.         }
  153.        
  154.         //
  155.         // *************************************************
  156.         // ** Static Runtime Functions to retrieve config **
  157.         // *************************************************
  158.         //
  159.        
  160.         public static NameValueCollection AppSettings {
  161.             get {
  162.                 object section = GetSection("appSettings");
  163.                 if (section == null || !(section is NameValueCollection)) {
  164.                     // If config is null or not the type we expect, the declaration was changed.
  165.                     // Treat it as a configuration error.
  166.                     throw new ConfigurationErrorsException(SR.GetString(SR.Config_appsettings_declaration_invalid));
  167.                 }
  168.                
  169.                 return (NameValueCollection)section;
  170.             }
  171.         }
  172.        
  173.         public static ConnectionStringSettingsCollection ConnectionStrings {
  174.             get {
  175.                 object section = GetSection("connectionStrings");
  176.                
  177.                 // Verify type, and return the collection
  178.                 if (section == null || section.GetType() != typeof(ConnectionStringsSection)) {
  179.                     // If config is null or not the type we expect, the declaration was changed.
  180.                     // Treat it as a configuration error.
  181.                     throw new ConfigurationErrorsException(SR.GetString(SR.Config_connectionstrings_declaration_invalid));
  182.                 }
  183.                
  184.                 ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)section;
  185.                 return connectionStringsSection.ConnectionStrings;
  186.             }
  187.         }
  188.        
  189.         // GetSection
  190.         //
  191.         // Retrieve a Section from the configuration system
  192.         //
  193.         public static object GetSection(string sectionName)
  194.         {
  195.             // Avoid unintended AV's by ensuring sectionName is not empty.
  196.             // For compatibility, we cannot throw an InvalidArgumentException.
  197.             if (String.IsNullOrEmpty(sectionName)) {
  198.                 return null;
  199.             }
  200.            
  201.             PrepareConfigSystem();
  202.            
  203.             object section = s_configSystem.GetSection(sectionName);
  204.             return section;
  205.         }
  206.        
  207.         public static void RefreshSection(string sectionName)
  208.         {
  209.             // Avoid unintended AV's by ensuring sectionName is not empty.
  210.             // For consistency with GetSection, we should not throw an InvalidArgumentException.
  211.             if (String.IsNullOrEmpty(sectionName)) {
  212.                 return;
  213.             }
  214.            
  215.             PrepareConfigSystem();
  216.            
  217.             s_configSystem.RefreshConfig(sectionName);
  218.         }
  219.        
  220.         //
  221.         // *************************************************
  222.         // ** Static Management Functions to edit config **
  223.         // *************************************************
  224.         //
  225.        
  226.         //
  227.         // OpenMachineConfiguration
  228.         //
  229.         public static Configuration OpenMachineConfiguration()
  230.         {
  231.             return OpenExeConfigurationImpl(null, true, ConfigurationUserLevel.None, null);
  232.         }
  233.        
  234.         public static Configuration OpenMappedMachineConfiguration(ConfigurationFileMap fileMap)
  235.         {
  236.             return OpenExeConfigurationImpl(fileMap, true, ConfigurationUserLevel.None, null);
  237.         }
  238.        
  239.         //
  240.         // OpenExeConfiguration
  241.         //
  242.         public static Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)
  243.         {
  244.             return OpenExeConfigurationImpl(null, false, userLevel, null);
  245.         }
  246.        
  247.         public static Configuration OpenExeConfiguration(string exePath)
  248.         {
  249.             return OpenExeConfigurationImpl(null, false, ConfigurationUserLevel.None, exePath);
  250.         }
  251.        
  252.         public static Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
  253.         {
  254.             return OpenExeConfigurationImpl(fileMap, false, userLevel, null);
  255.         }
  256.        
  257.         private static Configuration OpenExeConfigurationImpl(ConfigurationFileMap fileMap, bool isMachine, ConfigurationUserLevel userLevel, string exePath)
  258.         {
  259.             // exePath must be specified if not running inside ClientConfigurationSystem
  260.             if (!isMachine && (((fileMap == null) && (exePath == null)) || ((fileMap != null) && (((ExeConfigurationFileMap)fileMap).ExeConfigFilename == null)))) {
  261.                 if ((s_configSystem != null) && (s_configSystem.GetType() != typeof(ClientConfigurationSystem))) {
  262.                     throw new ArgumentException(SR.GetString(SR.Config_configmanager_open_noexe));
  263.                 }
  264.             }
  265.            
  266.             return ClientConfigurationHost.OpenExeConfiguration(fileMap, isMachine, userLevel, exePath);
  267.         }
  268.     }
  269. }

Developer Fusion