Example #1
0
        private static string GetRemoteConfigFile()
        {
            //TODO: we MUST create default configuration here in case that "RemoteConfigurationManagerConfiguration.config" does not exist
            string remoteFile = System.Configuration.ConfigurationManager.AppSettings[RemoteConfigFileAppSettingKey];

            if (remoteFile == null)
            {
                remoteFile = LocalConfigurationManager.MapConfigPath(RemoteConfigurationManagerConfigFileName);
            }
            else
            {
                remoteFile = LocalConfigurationManager.MapConfigPath(remoteFile);
            }
            if (!File.Exists(remoteFile))
            {
                Log("Config file '" + remoteFile + "' doesn't exists, use/create new configuration files in '" + ConfigUtility.DefaultApplicationConfigFolder + "'");
                remoteFile = ConfigUtility.Combine(ConfigUtility.DefaultApplicationConfigFolder, RemoteConfigurationManagerConfigFileName);
                if (!File.Exists(remoteFile))
                {
                    Directory.CreateDirectory(ConfigUtility.DefaultApplicationConfigFolder);
                    using (XmlTextWriter writer = new XmlTextWriter(remoteFile, Encoding.UTF8))
                    {
                        writer.WriteStartElement(RemoteConfigurationManagerConfiguration.TagName);
                        RemoteConfigurationManagerConfiguration.DefaultConfig.WriteXml(writer);
                        writer.WriteEndElement();
                        writer.Close();
                    }
                }
            }
            return(remoteFile);
        }
Example #2
0
 public static T GetSection <T>(string name, string path, out bool fromRemote)
 {
     if (System.IO.File.Exists(path))
     {
         fromRemote = false;
         return(XmlSerializerSectionHandler.CreateAndSetupWatcher <T>(LocalConfigurationManager.MapConfigPath(path)));
     }
     else
     {
         fromRemote = true;
         return(RemoteConfigurationManager.Instance.GetSection <T>(name));
     }
 }
Example #3
0
        object CreateLocalObject(Type type, string path, out int major, out int minor)
        {
            try
            {
                if (File.Exists(path))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    object obj = XmlSerializerSectionHandler.CreateAndSetupWatcher(doc.DocumentElement,
                                                                                   path, type, OnConfigFileChanged);

                    XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor);
                    return(obj);
                }

                //相对目录 自动同步xml文件
                string source   = "SourceV2";
                string fileName = Path.GetFileName(path);
                string mapPath  = LocalConfigurationManager.MapConfigPath("");
                if (mapPath.IndexOf(source) > 0)
                {
                    var sourceDir      = mapPath.Substring(0, mapPath.IndexOf(source) + source.Length);
                    var sourceFilePath = Path.Combine(sourceDir, "MB.configs//" + fileName);
                    if (File.Exists(sourceFilePath))
                    {
                        File.Copy(sourceFilePath, path, true);
                        if (File.Exists(path))
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.Load(path);
                            object obj = XmlSerializerSectionHandler.CreateAndSetupWatcher(doc.DocumentElement,
                                                                                           path, type, OnConfigFileChanged);

                            XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor);
                            return(obj);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, "RemoteConfigurationManager.CreateLocalObject,type=" + type.Name + ",path=" + path, type.Name);
            }
            major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type);
            minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion;
            return(null);
        }
Example #4
0
        static LocalConfigurationManager()
        {
            _systemConfig         = GetExeConfig();
            localBaseConfigFolder = Path.GetDirectoryName(_systemConfig.FilePath);
            instance = new LocalConfigurationManager();
            XmlDocument doc = new XmlDocument();

            if (File.Exists(_systemConfig.FilePath))
            {
                doc.Load(_systemConfig.FilePath);
            }
            else
            {
                doc.LoadXml("<configuration/>");
            }
            _systemConfigXml = doc;
        }