/// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationSettingsReader"/> class.
        /// </summary>
        /// <param name="sectionName">Name of the configuration section.</param>
        /// <param name="configurationFile">The configuration file.</param>
        public ConfigurationSettingsReader(string sectionName, string configurationFile)
        {
            if (sectionName == null)
            {
                throw new ArgumentNullException("sectionName");
            }
            if (configurationFile == null)
            {
                throw new ArgumentNullException("configurationFile");
            }

            if (!Path.IsPathRooted(configurationFile))
            {
                configurationFile = Path.Combine(_configurationDirectory, configurationFile);
            }

            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            map.ExeConfigFilename = configurationFile;

            var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

            _sectionHandler = (SectionHandler)configuration.GetSection(sectionName);

            if (_sectionHandler == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          ConfigurationSettingsReaderResources.SectionNotFound, sectionName));
            }
        }
Пример #2
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);
        }
Пример #3
0
 /// <summary>
 /// Registers individual configured modules 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 parsing the module configuration into registrations.
 /// </exception>
 /// <remarks>
 /// <para>
 /// This is where the individually configured component registrations get added to the <paramref name="builder" />.
 /// The <see cref="Autofac.Configuration.SectionHandler.Modules"/> collection from the <paramref name="configurationSection" />
 /// get processed into individual modules which are instantiated and activated inside the <paramref name="builder" />.
 /// </para>
 /// </remarks>
 protected virtual void RegisterConfiguredModules(ContainerBuilder builder, SectionHandler configurationSection)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     if (configurationSection == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     foreach (ModuleElement moduleElement in configurationSection.Modules)
     {
         var     moduleType = this.LoadType(moduleElement.Type, configurationSection.DefaultAssembly);
         IModule module     = null;
         using (var moduleActivator = new ReflectionActivator(
                    moduleType,
                    new DefaultConstructorFinder(),
                    new MostParametersConstructorSelector(),
                    moduleElement.Parameters.ToParameters(),
                    moduleElement.Properties.ToParameters()))
         {
             module = (IModule)moduleActivator.ActivateInstance(new ContainerBuilder().Build(), Enumerable.Empty <Parameter>());
         }
         builder.RegisterModule(module);
     }
 }
Пример #4
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);
 }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationSettingsReader"/> class.
        /// </summary>
        /// <param name="sectionName">Name of the configuration section.</param>
        public ConfigurationSettingsReader(string sectionName)
        {
            Enforce.ArgumentNotNull(sectionName, "sectionName");

            _sectionHandler = (SectionHandler)ConfigurationManager.GetSection(sectionName);

            if (_sectionHandler == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          ConfigurationSettingsReaderResources.SectionNotFound, sectionName));
            }
        }
Пример #6
0
 public virtual void RegisterConfigurationSection(ContainerBuilder builder, SectionHandler configurationSection)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     if (configurationSection == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     this.RegisterConfiguredModules(builder, configurationSection);
     this.RegisterConfiguredComponents(builder, configurationSection);
     this.RegisterReferencedFiles(builder, configurationSection);
 }
Пример #7
0
 /// <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);
     }
 }
Пример #8
0
        public static SectionHandler Deserialize(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            reader.MoveToContent();
            if (reader.EOF)
            {
                throw new ConfigurationErrorsException(ConfigurationSettingsReaderResources.NoXmlInConfiguration);
            }
            SectionHandler sectionHandler = new SectionHandler();

            sectionHandler.DeserializeElement(reader, false);
            return(sectionHandler);
        }
Пример #9
0
        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);
        }
Пример #10
0
 public ConfigurationSettingsReader(string sectionName, string configurationFile)
 {
     base.SectionHandler = SectionHandler.Deserialize(configurationFile, sectionName);
 }
Пример #11
0
        /// <summary>
        /// Registers individual configured components 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 parsing the component configuration into registrations.
        /// </exception>
        /// <remarks>
        /// <para>
        /// This is where the individually configured component registrations get added to the <paramref name="builder" />.
        /// The <see cref="Autofac.Configuration.SectionHandler.Components"/> collection from the <paramref name="configurationSection" />
        /// get processed into individual registrations with associated lifetime scope, name, etc.
        /// </para>
        /// <para>
        /// You may influence the process by overriding this whole method or by overriding these individual
        /// parsing subroutines:
        /// </para>
        /// <list type="bullet">
        /// <item>
        /// <term><see cref="Autofac.Configuration.ConfigurationRegistrar.SetLifetimeScope"/></term>
        /// </item>
        /// <item>
        /// <term><see cref="Autofac.Configuration.ConfigurationRegistrar.SetComponentOwnership"/></term>
        /// </item>
        /// <item>
        /// <term><see cref="Autofac.Configuration.ConfigurationRegistrar.SetInjectProperties"/></term>
        /// </item>
        /// </list>
        /// </remarks>
        protected virtual void RegisterConfiguredComponents(ContainerBuilder builder, SectionHandler configurationSection)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (configurationSection == null)
            {
                throw new ArgumentNullException("configurationSection");
            }
            foreach (ComponentElement component in configurationSection.Components)
            {
                var registrar = builder.RegisterType(LoadType(component.Type, configurationSection.DefaultAssembly));

                var services = this.EnumerateComponentServices(component, configurationSection.DefaultAssembly);
                foreach (var service in services)
                {
                    registrar.As(service);
                }
                foreach (var param in component.Parameters.ToParameters())
                {
                    registrar.WithParameter(param);
                }
                foreach (var prop in component.Properties.ToParameters())
                {
                    registrar.WithProperty(prop);
                }
                foreach (var ep in component.Metadata)
                {
                    registrar.WithMetadata(ep.Name, TypeManipulation.ChangeToCompatibleType(ep.Value, Type.GetType(ep.Type), null));
                }
                if (!string.IsNullOrEmpty(component.MemberOf))
                {
                    registrar.MemberOf(component.MemberOf);
                }
                this.SetLifetimeScope(registrar, component.InstanceScope);
                this.SetComponentOwnership(registrar, component.Ownership);
                this.SetInjectProperties(registrar, component.InjectProperties);
            }
        }
Пример #12
0
 /// <summary>
 /// Deserializes a configuration section handler from a specific block of XML.
 /// </summary>
 /// <param name="reader">
 /// The <see cref="System.Xml.XmlReader"/> used to read the XML configuration from the source.
 /// </param>
 /// <returns>
 /// A read/parsed <see cref="SectionHandler"/> based on the contents of the <paramref name="reader"/>.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="reader"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="System.Configuration.ConfigurationErrorsException">
 /// Thrown if <paramref name="reader"/> does not contain XML configuration that can be parsed into
 /// a <see cref="SectionHandler"/>.
 /// </exception>
 public static SectionHandler Deserialize(XmlReader reader)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     reader.MoveToContent();
     if (reader.EOF)
     {
         throw new ConfigurationErrorsException(ConfigurationSettingsReaderResources.NoXmlInConfiguration);
     }
     var handler = new SectionHandler();
     handler.DeserializeElement(reader, false);
     return handler;
 }
Пример #13
0
 public static SectionHandler Deserialize(string configurationFile)
 {
     return(SectionHandler.Deserialize(configurationFile, null));
 }
Пример #14
0
 protected virtual void RegisterConfiguredComponents(ContainerBuilder builder, SectionHandler configurationSection)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     if (configurationSection == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     foreach (ComponentElement current in configurationSection.Components)
     {
         IRegistrationBuilder <object, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder = RegistrationExtensions.RegisterType(builder, this.LoadType(current.Type, configurationSection.DefaultAssembly));
         IEnumerable <Service> enumerable = this.EnumerateComponentServices(current, configurationSection.DefaultAssembly);
         foreach (Service current2 in enumerable)
         {
             registrationBuilder.As(new Service[]
             {
                 current2
             });
         }
         foreach (Parameter current3 in current.Parameters.ToParameters())
         {
             RegistrationExtensions.WithParameter <object, ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current3);
         }
         foreach (Parameter current4 in current.Properties.ToParameters())
         {
             RegistrationExtensions.WithProperty <object, ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current4);
         }
         foreach (MetadataElement current5 in current.Metadata)
         {
             registrationBuilder.WithMetadata(current5.Name, TypeManipulation.ChangeToCompatibleType(current5.Value, Type.GetType(current5.Type), null));
         }
         if (!string.IsNullOrEmpty(current.MemberOf))
         {
             MemberOf <object, ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current.MemberOf);
         }
         this.SetLifetimeScope <ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current.InstanceScope);
         this.SetComponentOwnership <ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current.Ownership);
         this.SetInjectProperties <ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current.InjectProperties);
         this.SetAutoActivate <ConcreteReflectionActivatorData, SingleRegistrationStyle>(registrationBuilder, current.AutoActivate);
     }
 }