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

            ValidateConfiguration(file);
        }
        protected void ProtectSection(FileConfigurationSource source, string sectionName, string protectionProvider)
        {
            ConfigurationSectionCloner cloner = new ConfigurationSectionCloner();
            var section = source.GetSection(sectionName);
            section = cloner.Clone(section);

            source.Remove(sectionName);
            source.Add(sectionName, section, protectionProvider);
        }
 public void TryToSaveWithConfigurationMultipleTimes()
 {
     string tempFile = CreateFile();
     try
     {
         using (var source = new FileConfigurationSource(tempFile, false))
         {
             source.Add(InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection());
             ValidateConfiguration(tempFile);
             source.Add(InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection());
             ValidateConfiguration(tempFile);
             source.Add(InstrumentationConfigurationSection.SectionName, CreateInstrumentationSection());
             ValidateConfiguration(tempFile);
         }
     }
     finally
     {
         if (File.Exists(tempFile)) File.Delete(tempFile);
     }
 }
        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);
                }
            }
        }