/// <summary>
        /// 重写基类OnCreate方法来创建本地配置实例
        /// </summary>
        /// <param name="sectionName">节点名称</param>
        /// <param name="type">The type.</param>
        /// <param name="major">主版本号</param>
        /// <param name="minor">次版本号</param>
        /// <returns></returns>
        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, out major, out minor);

                // XmlSerializerSectionHandler.GetConfigVersion( doc.DocumentElement , out major , out minor );
            }
            catch (Exception ex)
            {
                HandleException(ex, "创建本地配置时出错: sectionName=" + sectionName + ",type=" + type.Name + ", 会创建空实例替代", sectionName);

                //出现异常通过反射初始化为空配置实例
                retVal = Activator.CreateInstance(type);
            }

            //为配置文件添加FileWatcher
            ConfigWatcher.SetupWatcher(configPath, retVal);
            return(retVal);
        }
Esempio n. 2
0
        private void LoadConfig()
        {
            SocketClientConfig config = Config;

            this.defaultMessageSettings = config.DefaultSocketSettings;
            XmlSerializerSectionHandler.RegisterReloadNotification(typeof(SocketClientConfig), new EventHandler(ReloadConfig));
        }
Esempio n. 3
0
 static EnvironmentMappingsConfig()
 {
     GetConfig();             //required to load the config so that the event fires.
     XmlSerializerSectionHandler.RegisterReloadNotification(typeof(EnvironmentMappingsConfig), (obj, args) =>
     {
         var cc = ConfigChanged;
         if (cc != null)
         {
             cc(GetConfig());
         }
     });
 }
        /// <summary>
        /// 验证远程配置文件的有效性
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="stream">The stream.</param>
        private void CheckDownloadStream(string sectionName, Stream stream)
        {
            ConfigEntry entry = this.GetEntry(sectionName);

            if (entry == null)
            {
                throw new System.Configuration.ConfigurationErrorsException("在 RemoteConfigurationManager 没有找到 '" + sectionName + "' ");
            }

            XmlDocument doc = new XmlDocument();

            doc.Load(stream);
            XmlSerializerSectionHandler.GetConfigInstance(doc.DocumentElement, entry.EntryType);
        }
        /// <include file='Avalara.AvaTax.Adapter.Doc.xml' path='adapter/common/members[@name="InternalConstructor"]/*' />
        public ServiceConfig()
        {
            string configFileName = Utilities.ConfigFileName();

            if (configFileName != null && configFileName != "")
            {
                _security = (RequestSecurity)XmlSerializerSectionHandler.CreateFromXmlFile(configFileName, "RequestSecurity");
            }

            if (_security == null)
            {
                //no config file or an invalid config file was found.
                //let's create a default one in case the consumer wants to fill in all fields manually
                _security = new RequestSecurity();
            }
        }
        /// <summary>
        /// 重写基类OnCreate方法来创建远程配置实例(原理其实还是把远程配置下载到本地创建本地配置实例)
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="type">The type.</param>
        /// <param name="major">The major.</param>
        /// <param name="minor">The minor.</param>
        /// <returns></returns>
        protected override object OnCreate(string sectionName, Type type, out int major, out int minor)
        {
            string fileName = GetFileName(sectionName);

            //string path = GetPath( sectionName );
            string path = ConfigUtility.Combine(config.LocalApplicationFolder, fileName);
            object obj  = CreateLocalObject(type, path, out major, out minor);

            if (obj != null)
            {
                return(obj);
            }

            major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type);
            minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion;
            try
            {
                RemoteConfigSectionParam newParams = GetServerVersion(sectionName, major);
                if (newParams != null)
                {
                    //从远程下载!
                    if (Download(newParams.SectionName, newParams.DownloadUrl, path, CheckDownloadStream))
                    {
                        obj = CreateLocalObject(type, path, out major, out minor);//下载远程配置到本地后创建配置实例
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, "从远程服务器下载配置 '" + sectionName + "' 时出错", sectionName);
            }

            //如果对象为null通过反射默认对象替代
            if (obj == null)
            {
                LoggerWrapper.Logger.Info("无法从 RemoteConfiguration 取得拥有类型'" + type.Name + "' 的'" + sectionName + "' 节点,通将过反射创建空实例替代");
                obj = Activator.CreateInstance(type);

                //为配置添加文件更改监控
                ConfigWatcher.SetupWatcher(path, obj);
                ConfigWatcher.RegisterReloadNotification(path, OnConfigFileChanged);
            }
            return(obj);
        }
        private void OnConfigFileChanged(object sender, EventArgs args)
        {
            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;
            }
        }
        /// <include file='Avalara.AvaTax.Adapter.Doc.xml' path='adapter/common/members[@name="Constructor"]/*' />
        public BaseSvc()
        {
            //this should be done first upon entering the assembly; expected that AddressSvc and TaxSvc will be
            //    the only instantiated classes and therefore its safe to do this here.

            _avaLog = AvaLogger.GetLogger();
            _avaLog.Debug(string.Format("Instantiating BaseSvc: {0}", _uniqueId));

            string configFileName = Utilities.ConfigFileName();

            if (configFileName != null && configFileName != "")
            {
                _config = (ServiceConfig)XmlSerializerSectionHandler.CreateFromXmlFile(configFileName, "ServiceConfig");
            }
            if (_config == null)
            {
                //no config file or an invalid config file was found.
                //let's create a default one in case the consumer wants to fill in all fields manually
                _config = new ServiceConfig();
            }
        }
        private 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 = ConfigWatcher.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);
        }
Esempio n. 10
0
 static SocketClient()
 {
     config = GetConfig();
     XmlSerializerSectionHandler.RegisterReloadNotification(typeof(SocketClientConfig), ReloadConfig);
 }