public void CanSaveConfigurationSectionToFile()
        {
            FileConfigurationSource source = new FileConfigurationSource(file, false);
            source.Save(InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection());

            ValidateConfiguration(file);
        }
 public void TryToSaveWithNullSectionThrows()
 {
     FileConfigurationSource source = new FileConfigurationSource(file, false);
     source.Save(InstrumentationConfigurationSection.SectionName, null);
 }
 public void TryToSaveWithNullOrEmptySectionNameThrows()
 {
     FileConfigurationSource source = new FileConfigurationSource(file, false);
     source.Save(null, CreateInstrumentationSection());
 }
        protected override void Arrange()
        {
            waitForChangedEvents = new CountdownEvent(2);
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(1000);

            originalConfigurationFileContents = File.ReadAllText(@"ExternalFileSource.config");

            using (var externalConfigurationFileSource = new FileConfigurationSource(@"ExternalFileSource.config", false))
            {
                externalConfigurationFileSource.Save(FileSourceDummySectionName, new DummySection { });
            }

            base.Arrange();

            this.CompositeSource.SourceChanged +=
                (sender, e) =>
                {
                    sourceChangedEvents++;
                    waitForChangedEvents.Signal();
                };
            this.CompositeSource.AddSectionChangeHandler(
                FileSourceDummySectionName,
                (sender, e) =>
                {
                    sectionChangedEvents++;
                    waitForChangedEvents.Signal();
                });
        }
        protected override void Arrange()
        {
            originalConfigurationFileContents = File.ReadAllText(@"ExternalFileSource.config");

            using (var externalConfigurationFileSource = new FileConfigurationSource(@"ExternalFileSource.config"))
            {
                externalConfigurationFileSource.Save(FileSourceDummySectionName, new DummySection { });
            }

            base.Arrange();
        }
        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 fileConfigurationSource =
                        new FileConfigurationSource(environmentDeltaFilePath, false);
                    if (!string.IsNullOrEmpty(protectionProvider))
                    {
                        fileConfigurationSource.Save(EnvironmentMergeSection.EnvironmentMergeData, environmentMergeSection, protectionProvider);
                    }
                    else
                    {
                        fileConfigurationSource.Add(EnvironmentMergeSection.EnvironmentMergeData, environmentMergeSection);
                    }
                }
                catch (ConfigurationErrorsException configurationErrors)
                {
                    errorService.LogErrors(configurationErrors);
                }
            }
        }