コード例 #1
0
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            if (!(data is NameValueCollection))
            {
                throw new ChoConfigurationException("Data object is not NameValueCollection object.");
            }
            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            NameValueCollection nameValueCollection = ((NameValueCollection)data);

            foreach (string key1 in nameValueCollection.Keys)
            {
                if (_configuration.AppSettings.Settings.AllKeys.Contains(key1))
                {
                    _configuration.AppSettings.Settings[key1].Value = nameValueCollection[key1];
                }
                else
                {
                    _configuration.AppSettings.Settings.Add(new KeyValueConfigurationElement(key1, nameValueCollection[key1]));
                }
            }

            _configuration.Save(ConfigurationSaveMode.Modified);
        }
コード例 #2
0
ファイル: ChoConfigStorage.cs プロジェクト: commodus/Cinchoo
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            ChoXmlDocument.CreateXmlFileIfEmpty(ConfigFilePath);

            string backupConfigFilePath = String.Format("{0}.{1}", ConfigFilePath, ChoReservedFileExt.Cho);

            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
                if (!IsAppConfigFile)
                {
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Archive);
                    }
                    File.Copy(ConfigFilePath, backupConfigFilePath, true);
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Hidden);
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            try
            {
                //if seperate config file maintained, make a link in the application configuration
                if (IsConfigReferenceDataChanged(stateInfo))
                {
                    using (ChoXmlDocument xmlDocument = new ChoXmlDocument(ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath))
                    {
                        XmlNode configNode = xmlDocument.XmlDocument.MakeXPath(ConfigElement.ConfigElementPath);
                        if (configNode != null)
                        {
                            if (!IsAppConfigFile)
                            {
                                string configXml = @"<{0} {1}=""{2}"" {3}=""{4}"" />".FormatString(ConfigSectionName, ChoConfigurationManager.PathToken, UnderlyingConfigFilePath, ChoXmlDocument.CinchoNSToken, ChoXmlDocument.CinchooNSURI);
                                ChoXmlDocument.SetNamespaceAwareOuterXml(configNode, configXml, ChoXmlDocument.CinchooNSURI);
                            }
                        }
                    }
                }

                PersistConfigData(data, stateInfo);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());

                if (!IsAppConfigFile)
                {
                    File.Copy(backupConfigFilePath, ConfigFilePath, true);
                }
            }
        }
コード例 #3
0
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            if (!(data is NameValueCollection))
            {
                throw new ChoConfigurationException("Data object is not NameValueCollection object.");
            }
            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            bool   saveMainConfig      = false;
            string appSettingsFilePath = this.GetAppSettingsFilePathIfAnySpecified();

            if (!appSettingsFilePath.IsNullOrWhiteSpace())
            {
                if (ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File) != ChoConfigurationManager.GetFullPath(appSettingsFilePath))
                {
                    saveMainConfig = true;
                    ChoConfigurationManager.ApplicationConfiguration.AppSettings.File = appSettingsFilePath;
                }
            }

            NameValueCollection nameValueCollection = ((NameValueCollection)data);

            if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.File.IsNullOrWhiteSpace())
            {
                foreach (string key1 in nameValueCollection.Keys)
                {
                    if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.AllKeys.Contains(key1))
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings[key1].Value = nameValueCollection[key1];
                    }
                    else
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.Add(new KeyValueConfigurationElement(key1, nameValueCollection[key1]));
                    }
                }

                ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
            }
            else
            {
                if (saveMainConfig)
                {
                    ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
                }
                File.WriteAllText(ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File), GetAppSettingsText(nameValueCollection));
            }
            //ConfigurationManager.RefreshSection(APP_SETTINGS_SECTION_NAME);
            ChoConfigurationManager.Refresh();
        }
コード例 #4
0
ファイル: ChoConfigSection.cs プロジェクト: commodus/Cinchoo
        public ChoConfigSection(Type configObjectType, XmlNode node, XmlNode[] contextNodes, ChoBaseConfigurationElement configElement)
            : this()
        {
            _configElement = configElement;

            if (configElement != null)
            {
                //Get and Set MetaDataFileName
                configElement.ConfigurationMetaDataType = ConfigurationMetaDataType;
                IChoConfigStorage attrDefinedConfigStorage = configElement.MetaDataInfo != null ? configElement.MetaDataInfo.ConfigStorage : null;

                ChoBaseConfigurationMetaDataInfo defaultMetaDataInfo = InitDefaultMetaDataInfo(configElement);

                configElement.MetaDataInfo = ChoObject.Merge <ChoBaseConfigurationMetaDataInfo>(ChoConfigurationMetaDataManager.GetMetaDataSection(configElement), defaultMetaDataInfo);
                ConfigStorage = configElement.MetaDataInfo != null && configElement.MetaDataInfo.ConfigStorage != null ?
                                configElement.MetaDataInfo.ConfigStorage : attrDefinedConfigStorage;

                if (ConfigStorage == null)
                {
                    configElement.Log("Missing configuration storage, assigning to configSection default storage.");
                    ConfigStorage = DefaultConfigStorage;
                    if (ConfigStorage == null)
                    {
                        configElement.Log("Missing configuration storage, assigning to system default storage.");
                        ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
                    }
                }

                if (ConfigStorage != null)
                {
                    try
                    {
                        CheckValidConfigStoragePassed(ConfigStorage);
                    }
                    catch (ChoFatalApplicationException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        if (configElement.Silent)
                        {
                            ConfigElement.Log(ex.Message);
                            ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
                            if (ConfigStorage != null)
                            {
                                ConfigElement.Log("Using default [{0}] config storage.".FormatString(ConfigStorage.GetType().FullName));
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                if (ConfigStorage == null)
                {
                    throw new ChoConfigurationException("Missing configuration storage.");
                }

                IsMetaDataDefinitionChanged = !ChoObject.Equals <ChoBaseConfigurationMetaDataInfo>(ChoConfigurationMetaDataManager.GetMetaDataSection(configElement), defaultMetaDataInfo);
                configElement.MetaDataInfo.ConfigStorage = ConfigStorage;

                try
                {
                    ConfigData = ConfigStorage.Load(_configElement, node);
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _configLoadExceptions.Add(ex);
                }
            }

            //if (ConfigStorage == null)
            //{
            //    try
            //    {
            //        ConfigStorage = DefaultConfigStorage;
            //        if (ConfigStorage == null)
            //            ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
            //    }
            //    catch (Exception ex)
            //    {
            //        _configLoadExceptions.Add(ex);
            //    }
            //}

            try
            {
                ConfigurationChangeWatcher = ConfigStorage.ConfigurationChangeWatcher;
                if (ConfigurationChangeWatcher != null)
                {
                    ConfigurationChangeWatcher.StartWatching();
                }
            }
            catch (Exception ex)
            {
                _configLoadExceptions.Add(ex);
            }
        }