public void CanDeserializeSerializedConfiguration() { string sourceName1 = "source1"; string sourceName2 = "source2"; string sourceFile1 = "file 1"; ConfigurationSourceSection settings = new ConfigurationSourceSection(); FileConfigurationSource.ResetImplementation(sourceFile1, false); ConfigurationSourceElement data1 = new FileConfigurationSourceElement(sourceName1, sourceFile1); ConfigurationSourceElement data2 = new SystemConfigurationSourceElement(sourceName2); settings.Sources.Add(data1); settings.Sources.Add(data2); settings.SelectedSource = sourceName1; IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>(); sections[ConfigurationSourceSection.SectionName] = settings; IConfigurationSource configurationSource = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections); ConfigurationSourceSection roSettigs = (ConfigurationSourceSection)configurationSource.GetSection(ConfigurationSourceSection.SectionName); Assert.IsNotNull(roSettigs); Assert.AreEqual(2, roSettigs.Sources.Count); Assert.AreEqual(sourceName1, roSettigs.SelectedSource); Assert.IsNotNull(roSettigs.Sources.Get(sourceName1)); Assert.AreSame(typeof(FileConfigurationSourceElement), roSettigs.Sources.Get(sourceName1).GetType()); Assert.AreEqual(sourceFile1, ((FileConfigurationSourceElement)roSettigs.Sources.Get(sourceName1)).FilePath); Assert.IsNotNull(roSettigs.Sources.Get(sourceName2)); Assert.AreSame(typeof(SystemConfigurationSourceElement), roSettigs.Sources.Get(sourceName2).GetType()); }
/// <summary> /// /// </summary> /// <param name="configurationFile"></param> public void Load(string configurationFile) { loading = true; try { FileConfigurationSource.ResetImplementation(configurationFile, false); IConfigurationUIHierarchyService hierarchyService = (IConfigurationUIHierarchyService)serviceProvider.GetService(typeof(IConfigurationUIHierarchyService)); foreach (ConfigurationUIHierarchy hierarchy in hierarchyService.GetAllHierarchies()) { hierarchyService.RemoveHierarchy(hierarchy); } ConfigurationApplicationFile data = new ConfigurationApplicationFile(Path.GetDirectoryName(configurationFile), configurationFile); ConfigurationUIHierarchy newhierarchy = new ConfigurationUIHierarchy(new ConfigurationApplicationNode(data), serviceProvider); hierarchyService.AddHierarchy(newhierarchy); hierarchyService.SelectedHierarchy = newhierarchy; configurationEditor.SetMainHierarchy(newhierarchy); newhierarchy.Open(); configurationEditor.ClearDirtyState(); } finally { loading = false; } }
public void RemoveIsReflectedInNextRequestWithoutRefresh() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationParameter parameter = new FileConfigurationParameter(otherConfigurationFile); FileConfigurationSource.ResetImplementation(otherConfigurationFile, false); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection otherSection = otherSource.GetSection(localSection) as DummySection; DummySection newSection = new DummySection(); newSection.Value = 13; otherSource.Add(parameter, localSection, newSection); otherSource.Remove(parameter, localSection); otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNull(otherSection); } finally { FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void DifferentFileConfigurationSourcesDoNotShareEvents() { ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); SystemConfigurationSource.ResetImplementation(true); File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); try { bool sysSourceChanged = false; bool otherSourceChanged = false; SystemConfigurationSource systemSource = new SystemConfigurationSource(); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection sysDummySection = systemSource.GetSection(localSection) as DummySection; DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection; Assert.IsTrue(sysDummySection != null); Assert.IsTrue(otherDummySection != null); systemSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args) { sysSourceChanged = true; }); otherSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args) { Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value); otherSourceChanged = true; }); DummySection rwSection = new DummySection(); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile); rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.SaveAs(otherConfigurationFile); Thread.Sleep(200); Assert.AreEqual(false, sysSourceChanged); Assert.AreEqual(true, otherSourceChanged); } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
/// <summary> /// Gets a <see cref="FileConfigurationSource"/> based on the applications configuration file. /// </summary> /// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param> /// <returns>A <see cref="FileConfigurationSource"/> based on the applications configuration file.</returns> protected override IConfigurationSource GetConfigurationSource(IServiceProvider serviceProvider) { string configurationFile = ServiceHelper.GetCurrentRootNode(serviceProvider).ConfigurationFile; FileConfigurationSource.ResetImplementation(configurationFile, false); return(new FileConfigurationSource(configurationFile)); }
public void ConfigurationFilePathsAreResolvedBeforeImplementationIsCreated() { string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; string configurationFilename = Path.GetFileName(fullConfigurationFilepath); FileConfigurationSource.ResetImplementation(fullConfigurationFilepath, false); BaseFileConfigurationSourceImplementation configSourceImpl1 = new FileConfigurationSource(configurationFilename).Implementation; BaseFileConfigurationSourceImplementation configSourceImpl2 = new FileConfigurationSource(fullConfigurationFilepath).Implementation; Assert.AreSame(configSourceImpl1, configSourceImpl2); }
public override void Save(IServiceProvider serviceProvider) { IErrorLogService errorService = ServiceHelper.GetErrorService(serviceProvider); IEnvironmentMergeService environmentMergeService = serviceProvider.GetService(typeof(IEnvironmentMergeService)) as IEnvironmentMergeService; if (environmentMergeService != null) { if (environmentMergeService.EnvironmentMergeInProgress) { return; } } IConfigurationUIHierarchy hierarchy = ServiceHelper.GetCurrentHierarchy(serviceProvider); Debug.Assert(hierarchy != null); ConfigurationApplicationNode configurationRootNode = hierarchy.FindNodeByType(typeof(ConfigurationApplicationNode)) as ConfigurationApplicationNode; Debug.Assert(configurationRootNode != null); string configurationFileDirectory = Path.GetDirectoryName(configurationRootNode.ConfigurationFile); foreach (EnvironmentNode environmentNode in hierarchy.FindNodesByType(typeof(EnvironmentNode))) { string environmentDeltaFilePath = Path.Combine(configurationFileDirectory, environmentNode.EnvironmentDeltaFile); Dictionary <string, ConfigurationNodeMergeData> mergeDataByPath = environmentNode.EnvironmentMergeData.UnfoldMergeData(hierarchy, false); EnvironmentMergeSection environmentMergeSection = new EnvironmentMergeSection(); environmentMergeSection.EnvironmentName = environmentNode.Name; environmentMergeSection.EnvironmentDeltaFile = environmentNode.EnvironmentConfigurationFile; CopyEnvironmentOverrides(environmentMergeSection, mergeDataByPath, hierarchy); string protectionProvider = GetProtectionProviderName(environmentNode); try { FileConfigurationSource.ResetImplementation(environmentDeltaFilePath, false); FileConfigurationSource fileConfigurationSource = new FileConfigurationSource(environmentDeltaFilePath); if (!string.IsNullOrEmpty(protectionProvider)) { fileConfigurationSource.Save(environmentDeltaFilePath, EnvironmentMergeSection.EnvironmentMergeData, environmentMergeSection, protectionProvider); } else { fileConfigurationSource.Save(environmentDeltaFilePath, EnvironmentMergeSection.EnvironmentMergeData, environmentMergeSection); } } catch (ConfigurationErrorsException configurationErrors) { errorService.LogErrors(configurationErrors); } } }
private static void UpdateConfigurationSource(IConfigurationUIHierarchy configurationUIHierarchy, string filename) { FileConfigurationSource.ResetImplementation(filename, false); configurationUIHierarchy.ConfigurationSource = new FileConfigurationSource(filename); configurationUIHierarchy.ConfigurationParameter = new FileConfigurationParameter(filename); configurationUIHierarchy.StorageService.ConfigurationFile = filename; if (configurationUIHierarchy.RootNode != null) { configurationUIHierarchy.RootNode.ConfigurationFile = filename; } }
public static IConfigurationSource SaveSectionsAndGetConfigurationSource(LoggingSettings loggingSettings) { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = "test.exe.config"; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); rwConfiguration.Sections.Remove(LoggingSettings.SectionName); rwConfiguration.Sections.Add(LoggingSettings.SectionName, loggingSettings); File.SetAttributes(fileMap.ExeConfigFilename, FileAttributes.Normal); rwConfiguration.Save(); rwConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); FileConfigurationSource.ResetImplementation(fileMap.ExeConfigFilename, false); return(new FileConfigurationSource(fileMap.ExeConfigFilename)); }
public void AddingUnprotectedSectionsWithProtectionProviderWillProtectThem() { DummySection dummySection = new DummySection(); string configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile); FileConfigurationParameter parameter = new FileConfigurationParameter(configurationFile); fileConfigSource.Add(parameter, protectedSection, dummySection, ProtectedConfiguration.DefaultProvider); FileConfigurationSource.ResetImplementation(configurationFile, true); ConfigurationSection section = fileConfigSource.GetSection(protectedSection); Assert.IsTrue(section.SectionInformation.IsProtected); Assert.IsNotNull(section.SectionInformation); Assert.AreEqual(ProtectedConfiguration.DefaultProvider, section.SectionInformation.ProtectionProvider.Name); }
public void ChangeInExternalConfigSourceIsDetected() { ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationParameter parameter = new FileConfigurationParameter(otherConfigurationFile); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection rwSection; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile); rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Save(ConfigurationSaveMode.Full); DummySection otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(12, otherSection.Value); rwSection.Value = 13; rwConfiguration.Save(ConfigurationSaveMode.Modified); Thread.Sleep(150); otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(13, otherSection.Value); } finally { ConfigurationChangeWatcher.ResetDefaultPollDelay(); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void RemovingSectionCausesChangeNotification() { ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); try { bool otherSourceChanged = false; FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection; Assert.IsTrue(otherDummySection != null); otherSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args) { Assert.IsNull(otherSource.GetSection(localSection)); otherSourceChanged = true; }); otherSource.Remove(otherConfigurationFile, localSection); Thread.Sleep(300); Assert.AreEqual(true, otherSourceChanged); } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void TryToSaveWithConfigurationMultipleTimes() { string tempFile = CreateFile(); try { FileConfigurationSource.ResetImplementation(tempFile, false); IConfigurationSource source = new FileConfigurationSource(tempFile); source.Add(new FileConfigurationParameter(tempFile), InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection()); ValidateConfiguration(tempFile); source.Add(new FileConfigurationParameter(tempFile), InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection()); ValidateConfiguration(tempFile); source.Add(new FileConfigurationParameter(tempFile), InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection()); ValidateConfiguration(tempFile); } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } }
public void SectionsCanBeAccessedThroughFileConfigurationSource() { string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; string otherConfigurationFilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); File.Copy(fullConfigurationFilepath, otherConfigurationFilepath); try { FileConfigurationSource.ResetImplementation(otherConfigurationFilepath, false); FileConfigurationSource otherConfiguration = new FileConfigurationSource(otherConfigurationFilepath); DummySection dummySection = otherConfiguration.GetSection(localSection) as DummySection; Assert.IsNotNull(dummySection); } finally { if (File.Exists(otherConfigurationFilepath)) { File.Delete(otherConfigurationFilepath); } } }
public void NonExistentSectionReturnsNullThroughFileConfigurationSource() { string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; string otherConfigurationFilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); File.Copy(fullConfigurationFilepath, otherConfigurationFilepath); try { FileConfigurationSource.ResetImplementation(otherConfigurationFilepath, false); FileConfigurationSource otherConfiguration = new FileConfigurationSource(otherConfigurationFilepath); object wrongSection = otherConfiguration.GetSection("wrong section"); Assert.IsNull(wrongSection); } finally { if (File.Exists(otherConfigurationFilepath)) { File.Delete(otherConfigurationFilepath); } } }
public void ReadsLatestVersionOnFirstRequest() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationSource.ResetImplementation(otherConfigurationFile, false); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection rwSection = null; ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = otherConfigurationFile; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); DummySection otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNotNull(otherSection); Assert.AreEqual(12, otherSection.Value); } finally { FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void Dispose() { FileConfigurationSource.ResetImplementation(this.configurationFileName, false); File.Delete(this.configurationFileName); }
public static IConfigurationSource GetConfigurationSourceForCustomFile(string fileName) { FileConfigurationSource.ResetImplementation(fileName, false); return(new FileConfigurationSource((fileName))); }