Beispiel #1
0
        public static SectionHandler Deserialize(string configurationFile, string configurationSection)
        {
            if (string.IsNullOrWhiteSpace(configurationSection))
            {
                configurationSection = "autofac";
            }
            configurationFile = SectionHandler.NormalizeConfigurationFilePath(configurationFile);
            ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap();

            exeConfigurationFileMap.ExeConfigFilename = configurationFile;
            System.Configuration.Configuration configuration = null;
            try
            {
                configuration = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
            }
            catch (ConfigurationErrorsException)
            {
                using (XmlTextReader xmlTextReader = new XmlTextReader(File.OpenRead(configurationFile)))
                {
                    SectionHandler result = SectionHandler.Deserialize(xmlTextReader);
                    return(result);
                }
            }
            SectionHandler sectionHandler = (SectionHandler)configuration.GetSection(configurationSection);

            if (sectionHandler == null)
            {
                throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, ConfigurationSettingsReaderResources.SectionNotFound, new object[]
                {
                    configurationSection
                }));
            }
            return(sectionHandler);
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XmlFileReader"/> class
 /// using a specified XML configuration file.
 /// </summary>
 /// <param name="fileName">
 /// The name of the configuration file containing XML that can deserialize into a <see cref="Autofac.Configuration.SectionHandler"/>.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="fileName" /> is <see langword="null" />.
 /// </exception>
 /// <exception cref="System.ArgumentException">
 /// Thrown if <paramref name="fileName" /> is empty.
 /// </exception>
 /// <remarks>
 /// <para>
 /// Relative paths may be specified in relation to the current application folder (where you would normally
 /// find <c>app.config</c> or <c>web.config</c>).
 /// </para>
 /// </remarks>
 public XmlFileReader(string fileName)
 {
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     if (fileName.Length == 0)
     {
         throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, ConfigurationSettingsReaderResources.ArgumentMayNotBeEmpty, "fileName"), "fileName");
     }
     this.SectionHandler = SectionHandler.Deserialize(fileName);
 }
 /// <summary>
 /// Registers referenced configuration files into a container builder.
 /// </summary>
 /// <param name="builder">
 /// The <see cref="Autofac.ContainerBuilder"/> that should receive the configured registrations.
 /// </param>
 /// <param name="configurationSection">
 /// The <see cref="Autofac.Configuration.SectionHandler"/> containing the configured registrations.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="builder"/> or <paramref name="configurationSection"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="System.Configuration.ConfigurationErrorsException">
 /// Thrown if there is any issue in processing the referenced files into registrations.
 /// </exception>
 /// <remarks>
 /// <para>
 /// This is where external files referenced in configuration get recursively loaded and added to the <paramref name="builder" />.
 /// The <see cref="Autofac.Configuration.SectionHandler.Files"/> collection from the <paramref name="configurationSection" />
 /// get processed into individual <see cref="Autofac.Configuration.SectionHandler"/> instances, each of which get
 /// registered with the <paramref name="builder" />.
 /// </para>
 /// </remarks>
 protected virtual void RegisterReferencedFiles(ContainerBuilder builder, SectionHandler configurationSection)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     if (configurationSection == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     foreach (FileElement file in configurationSection.Files)
     {
         var handler = SectionHandler.Deserialize(file.Name, file.Section);
         this.RegisterConfigurationSection(builder, handler);
     }
 }
        public static SectionHandler Deserialize(string configurationFile, string configurationSection)
        {
            if (String.IsNullOrWhiteSpace(configurationSection))
            {
                configurationSection = SectionHandler.DefaultSectionName;
            }

            // Normalizing the configuration file path also checks for null/empty.
            configurationFile = NormalizeConfigurationFilePath(configurationFile);
            var map = new ExeConfigurationFileMap();

            map.ExeConfigFilename = configurationFile;

            System.Configuration.Configuration configuration = null;
            try
            {
                configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            }
            catch (ConfigurationErrorsException)
            {
                // We have to fall back to "non-config XML file" like this to maintain some backwards-compatibility
                // with previous configuration mechanisms. The original way was "the file is a config file and we
                // can optionally pass a configuration section name." Thus, if there is no config section name passed,
                // we have to assume the old behavior and, failing that, try the new behavior.
                using (var reader = new XmlTextReader(File.OpenRead(configurationFile)))
                {
                    return(SectionHandler.Deserialize(reader));
                }
            }
            var handler = (SectionHandler)configuration.GetSection(configurationSection);

            if (handler == null)
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ConfigurationSettingsReaderResources.SectionNotFound, configurationSection));
            }
            return(handler);
        }
Beispiel #5
0
 public ConfigurationSettingsReader(string sectionName, string configurationFile)
 {
     base.SectionHandler = SectionHandler.Deserialize(configurationFile, sectionName);
 }
Beispiel #6
0
 public static SectionHandler Deserialize(string configurationFile)
 {
     return(SectionHandler.Deserialize(configurationFile, null));
 }