public void StorageFiresEventTest()
        {
            using (ConfigurationContext configurationContext = CreateConfigurationContext(XmlString))
            {
                StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
                IStorageProviderReader storage = factory.Create(applConfig1) as IStorageProviderReader;
                Assert.AreSame(storage.GetType(), typeof(XmlFileStorageProvider));

                using (IConfigurationChangeWatcher watcher = storage.CreateConfigurationChangeWatcher())
                {
                    watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(OnConfigurationChanged);

                    watcher.StartWatching();
                    Thread.Sleep(100);
                    ((IStorageProviderWriter)storage).Write(GetData());

                    for (int wait = 0; wait < 10 && eventFiredCount < 2; ++wait)
                    {
                        Thread.Sleep(500);
                    }

                    watcher.Dispose();
                }

                Assert.AreEqual(1, eventFiredCount);
            }
        }
Example #2
0
 public void CreateTest()
 {
     using (ConfigurationContext configurationContext = CreateConfigurationContext(xmlString))
     {
         StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
         IStorageProviderReader storage = factory.Create("ApplConfig1");
         Assert.IsNotNull(storage);
     }
 }
        private IStorageProviderWriter GetConfigurationStorageWriter(string sectionName)
        {
            IStorageProviderReader configStorageReader = CreateStorageProvider(sectionName);
            IStorageProviderWriter configStorageWriter = configStorageReader as IStorageProviderWriter;

            if (configStorageWriter == null)
            {
                throw new ConfigurationException(SR.ExceptionHandlerNotWritable(sectionName));
            }
            return(configStorageWriter);
        }
        /// <summary>
        /// <para>
        /// Gets a value indicating whether a section in configuration is read-only.
        /// </para>
        /// </summary>
        /// <param name="sectionName">
        /// <para>
        /// The section in the configuration.
        /// </para>
        /// </param>
        /// <returns>
        /// <see langword="true"/> if the configuration section is read-only; otherwise <see langword="false"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="sectionName"/> can not be <see langword="null"/>.</para>
        /// </exception>
        public bool IsReadOnly(string sectionName)
        {
            ValidateSectionNameNotNull(sectionName);
            IStorageProviderReader configProvider = CreateStorageProvider(sectionName);

            if (configProvider is IStorageProviderWriter)
            {
                return(false);
            }
            return(true);
        }
 public void NoSectionStorageTest()
 {
     using (ConfigurationContext configurationContext = CreateConfigurationContext(NoSectionXmlString))
     {
         StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
         IStorageProviderReader storage = factory.Create(applConfig1) as IStorageProviderReader;
         Assert.AreSame(storage.GetType(), typeof(XmlFileStorageProvider));
         storage.Read();
         Assert.Fail("Should never get here since the section is not specified in the external configuration file.");
     }
 }
 public void StorageTest()
 {
     using (ConfigurationContext configurationContext = CreateConfigurationContext(XmlString))
     {
         StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
         IStorageProviderReader storage = factory.Create(applConfig1) as IStorageProviderReader;
         Assert.AreSame(storage.GetType(), typeof(XmlFileStorageProvider));
         ((IStorageProviderWriter)storage).Write(GetData());
         XmlNode data = storage.Read() as XmlNode;
         Assert.AreEqual(GetData().OuterXml, data.OuterXml);
     }
 }
        public void StorageTestChangeEncryption()
        {
            SaveKeyAlgorithmPair(XmlStringWithEncryption);

            using (ConfigurationContext configurationContext = CreateConfigurationContext(XmlStringWithEncryption))
            {
                StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
                IStorageProviderReader storage = factory.Create(applConfig1) as IStorageProviderReader;
                Assert.AreSame(storage.GetType(), typeof(XmlFileStorageProvider));
                ((IStorageProviderWriter)storage).Write(GetData());

                configurationContext.GetMetaConfiguration().ConfigurationSections[0].Encrypt = false;
                factory = new StorageProviderFactory(configurationContext);
                IStorageProviderWriter storageWriter = factory.Create(applConfig1) as IStorageProviderWriter;
                storageWriter.Write(GetData());

                XmlNode data = storageWriter.Read() as XmlNode;
                Assert.AreEqual(GetData().OuterXml, data.OuterXml);
            }
        }
        public void StorageTestWithEncryption()
        {
            SaveKeyAlgorithmPair(XmlStringWithEncryption);
            using (ConfigurationContext configurationContext = CreateConfigurationContext(XmlStringWithEncryption))
            {
                using (FileStream s = new FileStream(Path.GetFullPath(testConfigFile), FileMode.Create))
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        byte b = 0;
                        s.WriteByte(b);
                    }
                }

                StorageProviderFactory factory = new StorageProviderFactory(configurationContext);
                IStorageProviderReader storage = factory.Create(applConfig1) as IStorageProviderReader;
                Assert.AreSame(storage.GetType(), typeof(XmlFileStorageProvider));
                ((IStorageProviderWriter)storage).Write(GetData());
                XmlNode data = storage.Read() as XmlNode;
                Assert.AreEqual(GetData().OuterXml, data.OuterXml);
            }
        }
        /// <summary>
        /// <para>
        /// Reads configuration settings for a user-defined configuration section.
        /// </para>
        /// </summary>
        /// <param name="sectionName">
        /// <para>The configuration section to read.</para>
        /// </param>
        /// <returns>
        /// <para>The configuration settings for <paramref name="sectionName"/>.</para>
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <para><paramref name="sectionName"/>can not be <see langword="null"/> (Nothing in Visual Basic).</para>
        /// </exception>
        /// <exception cref="ConfigurationException">
        /// <para><paramref name="sectionName"/> is not valid for this configuration.</para>
        /// </exception>
        public object ReadConfiguration(string sectionName)
        {
            ValidateSection(sectionName);

            object configurationSection = sections.GetSection(sectionName);

            if (IsConfigurationSectionCached(configurationSection))
            {
                return(configurationSection);
            }

            IStorageProviderReader storageProviderReader = CreateStorageProvider(sectionName);

            object configurationSettings = storageProviderReader.Read();

            if (configurationSettings == null)
            {
                return(null);
            }

            ITransformer transformer = CreateTransformer(sectionName);

            if (transformer != null)
            {
                configurationSection = transformer.Deserialize(configurationSettings);
            }
            else
            {
                configurationSection = configurationSettings;
            }

            ConfigurationChangedEventHandler changed = new ConfigurationChangedEventHandler(OnExternalConfigurationChanged);

            sections.AddSection(sectionName, configurationSection, changed, storageProviderReader);

            return(configurationSection);
        }