private RemoteConfigSectionCollection GetServerVersions(RemoteConfigSectionCollection lstParams)
        {
            // send
            // <config machine=''application=''>
            //      <section name='' majorVerion='' minorVersion='' />
            //      <section name='' majorVerion='' minorVersion='' />
            //      <section name='' majorVerion='' minorVersion='' />
            // </config>
            //
            // receive
            // <config>
            //      <section name='' majorVerion='' minorVersion='' downloadUrl='' />
            //      <section name='' majorVerion='' minorVersion='' downloadUrl='' />
            //      <section name='' majorVerion='' minorVersion='' downloadUrl='' />
            // </config>

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlSerializer ser = new XmlSerializer(typeof(RemoteConfigSectionCollection));
                    ser.Serialize(ms, lstParams);

                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(config.RemoteConfigurationUrl);
                    req.ContentType      = "text/xml";
                    req.Method           = "POST";
                    req.Timeout          = config.Timeout;
                    req.ReadWriteTimeout = config.ReadWriteTimeout;
                    req.ContentLength    = ms.Length;
                    req.ServicePoint.Expect100Continue = false;
                    req.ServicePoint.UseNagleAlgorithm = false;
                    req.KeepAlive = false;

                    using (Stream stream = req.GetRequestStream())
                    {
                        byte[] buf = ms.ToArray();
                        stream.Write(buf, 0, buf.Length);
                    }
                    RemoteConfigSectionCollection newParams;
                    using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
                    {
                        using (Stream stream = rsp.GetResponseStream())
                        {
                            newParams = (RemoteConfigSectionCollection)ser.Deserialize(stream);
                        }
                    }
                    return(newParams);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, "无法从 '" + config.RemoteConfigurationUrl + "'获得远程服务器上的版本", "RemoteConfigurationManager");
                return(new RemoteConfigSectionCollection());
            }
        }
示例#2
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string strCurrentPath = Request.Path.ToLower();

            //if (strCurrentPath.IndexOf("configversionhandler.ashx") >= 0)
            //    ConfigVersionCounter.Increment();
            //else if (strCurrentPath.IndexOf("resourcemanagerhandler.ashx") >= 0)
            //    ResourceMgrCounter.Increment();
            //else if (strCurrentPath.IndexOf("configmanagerhandler.ashx") >= 0)
            //    ConfigMgrCounter.Increment();

            if (bool.Parse(ConfigurationManager.AppSettings["debug"]))
            {
                string Info = string.Empty;
                if (strCurrentPath != null)
                {
                    Info = string.Format("Begin Request: {0}", strCurrentPath);

                    try
                    {
                        if (strCurrentPath.ToLower().IndexOf("configversionhandler.ashx") >= 0)
                        {
                            XmlSerializer xser = new XmlSerializer(typeof(RemoteConfigSectionCollection));
                            RemoteConfigSectionCollection rcc = (RemoteConfigSectionCollection)xser.Deserialize(Request.InputStream);
                            Request.InputStream.Seek(0, SeekOrigin.Begin);
                            Info += string.Format("\r\n\tApplication:{0} Machine:{1}", rcc.Application, rcc.Machine);
                        }
                        else if ((strCurrentPath.ToLower().IndexOf("resourcemanagerhandler.ashx") >= 0) || (strCurrentPath.ToLower().IndexOf("configmanagerhandler.ashx") >= 0))
                        {
                            XmlSerializer          xser = new XmlSerializer(typeof(RemoteConfigManagerDTO));
                            RemoteConfigManagerDTO rcm  = (RemoteConfigManagerDTO)xser.Deserialize(Request.InputStream);
                            Request.InputStream.Seek(0, SeekOrigin.Begin);
                            Info += string.Format("\r\n\tCommand:{0} OperatorID:{1} Application:{2} Machine:{3}", rcm.Operation.Command, rcm.Operation.OperatorID, rcm.RemoteConfigSections.Application, rcm.RemoteConfigSections.Machine);
                        }
                    }
                    catch (Exception err)
                    {
                        if (Request.InputStream != null)
                        {
                            Request.InputStream.Seek(0, SeekOrigin.Begin);
                            byte[] bytes = new byte[Request.InputStream.Length + 1];
                            Request.InputStream.Read(bytes, 0, (int)(Request.InputStream.Length));
                            string streamContent = new UTF8Encoding().GetString(bytes);
                            Request.InputStream.Seek(0, SeekOrigin.Begin);

                            logger.Error(streamContent, err);
                        }
                    }

                    logger.Info(Info);
                }
            }
        }
        public void RefreshAllConfigs()
        {
            RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);

            lock (configLocker)
            {
                foreach (ConfigEntry entry in configEntries.Values)
                {
                    if (entry.IsSet)
                    {
                        lstParams.AddSection(entry.Name, entry.MajorVersion, entry.MinorVersion);
                    }
                }
            }
            RefreshConfigs(lstParams);
        }
        /// <summary>
        /// 取得服务器上的配置版本
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="majorVersion">The major version.</param>
        /// <returns></returns>
        private RemoteConfigSectionParam GetServerVersion(string name, int majorVersion)
        {
            RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName, config.Environment);

            lstParams.AddSection(name, majorVersion, XmlSerializerSectionHandler.DefaultUninitMinorVersion);
            RemoteConfigSectionCollection newParams = GetServerVersions(lstParams);

            if (newParams.Count == 0)
            {
                return(null);
            }
            else
            {
                return(newParams[0]);
            }
        }
        private void TimerCallback(object sender, System.Timers.ElapsedEventArgs args)
        {
            RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);

            //configLocker.AcquireReaderLock(-1);
            //using (configLocker.AcquireReaderLock())
            lock (configLocker)
            {
                foreach (ConfigEntry entry in configEntries.Values)
                {
                    if (entry.IsSet)
                    {
                        lstParams.AddSection(entry.Name, entry.MajorVersion, entry.MinorVersion);
                    }
                }

                //configLocker.ReleaseReaderLock();
            }
            if (lstParams.Count == 0)
            {
                return;
            }

            //发送本地版本信息到服务器获取更新信息
            lstParams = GetServerVersions(lstParams);
            if (lstParams.Count == 0)
            {
                return;
            }

            //如果有更新则下载新的配置文件
            foreach (RemoteConfigSectionParam param in lstParams.Sections)
            {
                ThreadPool.QueueUserWorkItem(
                    delegate(object obj)
                {
                    DownloadParam dp = (DownloadParam)obj;
                    Download(dp.Name, dp.Url, dp.LocalPath, dp.Checker);
                },
                    new DownloadParam(param.SectionName,
                                      param.DownloadUrl,
                                      GetPath(param.SectionName),
                                      CheckDownloadStream)
                    );
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.InputStream.Length == 0)
            {
                return;
            }
            XmlSerializer xser = new XmlSerializer(typeof(RemoteConfigSectionCollection));
            RemoteConfigSectionCollection rcc = (RemoteConfigSectionCollection)xser.Deserialize(context.Request.InputStream);

            RemoteConfigSectionCollection ret = new RemoteConfigSectionCollection();
            bool exitAppConfig = false;

            foreach (RemoteConfigSectionParam param in rcc)
            {
                int configType;
                if (!string.IsNullOrEmpty(rcc.Application))
                {
                    int minor = GetLastVersion(param.SectionName, rcc.Application, param.MajorVersion, out configType, out exitAppConfig);
                    if (minor > param.MinorVersion)
                    {
                        string url = GetDownloadUrl(param.SectionName, rcc.Application, param.MajorVersion, minor, configType);
                        ret.AddSection(param.SectionName, param.MajorVersion, minor, url);
                        continue;
                    }
                }
                //如果指定了应用程序,且应用程序有配置文件,就不再处理默认配置文件
                if (exitAppConfig)
                {
                    continue;
                }
                int minor2 = GetLastVersion(param.SectionName, NoAppPath, param.MajorVersion, out configType, out exitAppConfig);
                if (minor2 > param.MinorVersion)
                {
                    string url = GetDownloadUrl(param.SectionName, NoAppPath, param.MajorVersion, minor2, configType);
                    ret.AddSection(param.SectionName, param.MajorVersion, minor2, url);
                }
            }

            context.Response.ContentType = "text/xml";
            xser.Serialize(context.Response.OutputStream, ret);
        }
        private void RefreshConfigs(RemoteConfigSectionCollection lstParams)
        {
            if (lstParams.Count == 0)
            {
                return;
            }
            RemoteConfigSectionCollection newParams = GetServerVersions(lstParams); //取得服务器上的版本信息

            if (newParams.Count == 0)                                               //没有新版本直接返回
            {
                return;
            }

            //用来记录日志
            Dictionary <string, RemoteConfigSectionParam> tblOldParam = new Dictionary <string, RemoteConfigSectionParam>(lstParams.Count);

            foreach (RemoteConfigSectionParam item in lstParams)
            {
                tblOldParam.Add(item.SectionName, item);
            }

            foreach (RemoteConfigSectionParam param in newParams.Sections)
            {
                string localPath = GetPath(param.SectionName);

                //下载远程配置到本地
                if (!Download(param.SectionName, param.DownloadUrl, localPath, CheckDownloadStream))
                {
                    throw new System.Configuration.ConfigurationErrorsException("无法下载 '" + param.DownloadUrl + "' 到 '" + localPath + "'");
                }

                //执行本地配置更改监听事件
                FileWatcher.Instance.ProcessFileChanged(localPath);

                LoggerWrapper.Logger.Info(string.Format("配置 '{0}({1})' 已经从版本({2})更新到版本({3})",
                                                        param.SectionName, param.MajorVersion,
                                                        tblOldParam[param.SectionName].MinorVersion, param.MinorVersion));
            }
        }
        public void RefreshConfig(string name, int majorVersion)
        {
            RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);
            int orgMinorVersion = 0;

            lock (configLocker)
            {
                foreach (ConfigEntry entry in configEntries.Values)
                {
                    if (entry.Name == name)
                    {
                        if (entry.IsSet && entry.MajorVersion == majorVersion)
                        {
                            orgMinorVersion = entry.MinorVersion;
                            lstParams.AddSection(entry.Name, entry.MajorVersion, entry.MinorVersion);
                        }
                        break;
                    }
                }
            }
            RefreshConfigs(lstParams);
        }
 public RemoteConfigManagerDto()
 {
     Operation            = new RemoteConfigOperation();
     RemoteConfigSections = new RemoteConfigSectionCollection();
 }
示例#10
0
 public RemoteConfigManagerDTO()
 {
     operation            = new RemoteConfigOperation();
     remoteConfigSections = new RemoteConfigSectionCollection();
 }