예제 #1
0
        protected override object OnCreate(string sectionName, Type type, out int major, out int minor)
        {
            major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type);
            minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion;
            string configPath = GetConfigSectionFileName(sectionName);

            if (configPath.Length == 0)
            {
                return(null);
            }

            object retVal;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(configPath);

                retVal = XmlSerializerSectionHandler.GetConfigInstance(doc.DocumentElement, type);
                XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor);
            }
            catch (Exception ex)
            {
                HandleException(ex, "Error when create local configuration: sectionName=" + sectionName + ",type=" + type.Name + ", create entry config instead", sectionName);
                //if exception here, return default configuration class and then setup watch
                retVal = Activator.CreateInstance(type);
            }

            XmlSerializerSectionHandler.SetupWatcher(configPath, retVal);
            return(retVal);
        }
예제 #2
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);
        }
예제 #3
0
        void OnConfigFileChanged(object sender, EventArgs args)
        {
            try
            {
                string filePath    = ((FileChangedEventArgs)args).FileName;
                string sectionName = GetSectionName(filePath);

                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                int major, minor;
                XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor);

                ConfigEntry entry = GetEntry(sectionName);
                if (entry != null)
                {
                    entry.MinorVersion = minor;
                }
            }
            catch (Exception ex)
            {
                logger.HandleException(ex, "RemoteConfigurationManager");
            }
        }