public override void Register(IServiceProvider serviceProvider)
        {
            new EnvironmentalOverridesCommandRegistrar(serviceProvider).Register();

            if (serviceProvider.GetService(typeof(IEnvironmentMergeService)) == null)
            {
                IServiceContainer serviceContainer = (IServiceContainer)serviceProvider.GetService(typeof(IServiceContainer));

                EnvironmentMergeAware environmentMergeService = new EnvironmentMergeAware();
                serviceContainer.AddService(typeof(IEnvironmentMergeService), environmentMergeService);
                serviceContainer.AddService(typeof(EnvironmentMergeAware), environmentMergeService);
            }

            ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
            if (rootNode != null)
            {
                if (rootNode.Nodes[Resources.EnvironmentalOverridesNodeName] == null)
                {
                    rootNode.AddNode(new EnvironmentalOverridesNode());
                }
            }
        }
Exemple #2
0
        public override void Register(IServiceProvider serviceProvider)
        {
            new EnvironmentalOverridesCommandRegistrar(serviceProvider).Register();

            if (serviceProvider.GetService(typeof(IEnvironmentMergeService)) == null)
            {
                IServiceContainer serviceContainer = (IServiceContainer)serviceProvider.GetService(typeof(IServiceContainer));

                EnvironmentMergeAware environmentMergeService = new EnvironmentMergeAware();
                serviceContainer.AddService(typeof(IEnvironmentMergeService), environmentMergeService);
                serviceContainer.AddService(typeof(EnvironmentMergeAware), environmentMergeService);
            }

            ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);

            if (rootNode != null)
            {
                if (rootNode.Nodes[Resources.EnvironmentalOverridesNodeName] == null)
                {
                    rootNode.AddNode(new EnvironmentalOverridesNode());
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Performs the merging of configuration, given a specific <see cref="EnvironmentNode"/>.
        /// </summary>
        /// <param name="node">The <see cref="EnvironmentNode"/> this command should be executed on.</param>
        protected override void ExecuteCore(ConfigurationNode node)
        {
            EnvironmentNode environmentNode = node as EnvironmentNode;

            if (environmentNode != null)
            {
                if (EnsureMainHierarchyIsSaved(node.Hierarchy, node.Site))
                {
                    EnvironmentMergeAware environmentMergeService = node.Site.GetService(typeof(EnvironmentMergeAware)) as EnvironmentMergeAware;

                    string originalFilePath      = Path.GetFullPath(node.Hierarchy.RootNode.ConfigurationFile);
                    string originalDirectoryPath = Path.GetDirectoryName(originalFilePath);
                    environmentMergeService.SetEnvironmentalMergeInProgress(true);

                    try
                    {
                        UIService.BeginUpdate();

                        Dictionary <string, ConfigurationNodeMergeData> mergeDataByPath = environmentNode.EnvironmentMergeData.UnfoldMergeData(environmentNode.Hierarchy, false);

                        string temporaryFilepath       = Path.GetTempFileName();
                        bool   temporarySaveSuccessful = false;
                        try
                        {
                            UpdateConfigurationSource(node.Hierarchy, temporaryFilepath);

                            SaveConfigurationApplicationNodeCommand saveConfigurationNodeCommand = new SaveConfigurationApplicationNodeCommand(ServiceProvider);
                            saveConfigurationNodeCommand.Execute(ServiceHelper.GetCurrentRootNode(ServiceProvider));
                            temporarySaveSuccessful = saveConfigurationNodeCommand.SaveSucceeded;
                        }
                        finally
                        {
                            UpdateConfigurationSource(node.Hierarchy, originalFilePath);
                            node.Hierarchy.ConfigurationSource    = null;
                            node.Hierarchy.ConfigurationParameter = null;
                        }

                        if (temporarySaveSuccessful)
                        {
                            using (TemporaryConfigurationHierarchy tempHierarchy = new TemporaryConfigurationHierarchy(ServiceProvider, temporaryFilepath))
                            {
                                MergeHierarchy(tempHierarchy.Hierarchy, mergeDataByPath);

                                SaveConfigurationApplicationNodeCommand saveConfigurationApplication = new SaveConfigurationApplicationNodeCommand(ServiceProvider);

                                string environmentConfigurationFile = CreateMergedConfigurationFile(environmentNode, originalFilePath, originalDirectoryPath);

                                UpdateConfigurationSource(tempHierarchy.Hierarchy, environmentConfigurationFile);
                                RemoveConfigurationSourceElements(tempHierarchy.Hierarchy);

                                saveConfigurationApplication.Execute(tempHierarchy.Hierarchy.RootNode);

                                mergeSucceeded          = saveConfigurationApplication.SaveSucceeded;
                                mergedConfigurationFile = environmentConfigurationFile;
                            }

                            try
                            {
                                File.Delete(temporaryFilepath);
                            }
                            catch (IOException)
                            {
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        IUIService uiService = ServiceHelper.GetUIService(ServiceProvider);
                        uiService.ShowError(e, Resources.ErrorSavingMergedConfigurationCaption);
                    }
                    finally
                    {
                        IConfigurationUIHierarchyService hierarchyService = ServiceHelper.GetUIHierarchyService(ServiceProvider);
                        hierarchyService.SelectedHierarchy = node.Hierarchy;

                        UIService.EndUpdate();

                        environmentMergeService.SetEnvironmentalMergeInProgress(false);
                    }
                }
            }
        }