public void InvalidAllConfigs()
 {
     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);
         }
     }
     InvalidConfigs(lstParams);
 }
 public void InvalidConfig(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;
             }
         }
     }
     InvalidConfigs(lstParams);
 }
        private void TimerCallback(object sender, System.Timers.ElapsedEventArgs args)
        {
            RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);
            //configLocker.AcquireReaderLock(-1);
            //using (configLocker.AcquireReaderLock())
            if (!timerRunning)
            {
                lock (configLocker)
                {
                    timerRunning = true;
                    foreach (ConfigEntry entry in configEntries.Values)
                    {
                        if (entry.IsSet)
                            lstParams.AddSection(entry.Name, entry.MajorVersion, entry.MinorVersion);
                    }
                    //configLocker.ReleaseReaderLock();

                    if (lstParams.Count != 0)
                    {
                        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)
                                    );
                        }
                    }
                    timerRunning = false;
                }
            }
        }
        private void TimerCallback(object stat = null)
        {
            if (!timerRunning)
            {
                lock (configLocker)
                {
                    timerRunning = true;
                    try
                    {
                        //Log(string.Format("start Time:{0}", DateTime.Now.ToString()));
                        RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);
                        //configLocker.AcquireReaderLock(-1);
                        //using (configLocker.AcquireReaderLock())

                        foreach (ConfigEntry entry in configEntries.Values)
                        {
                            if (entry.IsSet)
                                lstParams.AddSection(entry.Name, entry.MajorVersion, entry.MinorVersion);
                        }
                        //configLocker.ReleaseReaderLock();
                        if (lstParams.Count != 0)
                        {
                            lstParams = GetServerVersions(lstParams);
                            if (lstParams.Count != 0)
                            {
                                Log(string.Format("获得新的配置文件:SectionName:{0} MajorVersion:{1} MinorVersion:{2}", lstParams[0].SectionName, lstParams[0].MajorVersion, lstParams[0].MinorVersion));
                                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)
                                            );
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log(ex.ToString());
                    }
                    finally
                    {
                        timer.Change(config.TimerInterval, Timeout.Infinite);
                        timerRunning = false;
                    }
                }
            }
        }
        private void InvalidConfigs(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("Unabled to download '" + param.DownloadUrl + "' to '" + localPath + "'");
                }
                FileWatcher.Instance.ProcessFileChanged(localPath);

                Log(string.Format("Minor version of config '{0}({1})' has been updated manually from {2} to {3}",
                            param.SectionName, param.MajorVersion,
                            tblOldParam[param.SectionName].MinorVersion, param.MinorVersion));
            }
        }
        private RemoteConfigSectionCollection GetServerVersions(RemoteConfigSectionCollection lstInputParams)
        {
            MemoryStream ms = null;
            XmlSerializer ser = null;
            HttpWebRequest req = null;
            try
            {
                ms = new MemoryStream();
                ser = new XmlSerializer(typeof(RemoteConfigSectionCollection));
                ser.Serialize(ms, lstInputParams);

                req = (HttpWebRequest)HttpWebRequest.Create(config.RemoteConfigurationUrl);
                req.ContentType = "text/xml";
                req.Proxy = null;
                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);
                    stream.Close();
                }

                RemoteConfigSectionCollection lstOutput;
                using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream stream = rsp.GetResponseStream())
                    {
                        lstOutput = (RemoteConfigSectionCollection)ser.Deserialize(stream);
                        stream.Close();
                    }
                    rsp.Close();
                }
                return lstOutput;
            }
            catch (Exception ex)
            {
                HandleException(ex, "Unabled to GetServerVersions from '" + config.RemoteConfigurationUrl + "'", "RemoteConfigurationManager");
                return new RemoteConfigSectionCollection();
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                    ms = null;
                }
                req = null;
                ser = null;
            }
        }
 private RemoteConfigSectionParam GetServerVersion(string name, int majorVersion)
 {
     RemoteConfigSectionCollection lstParams = new RemoteConfigSectionCollection(config.ApplicationName);
     lstParams.AddSection(name, majorVersion, XmlSerializerSectionHandler.DefaultUninitMinorVersion);
     lstParams = GetServerVersions(lstParams);
     if (lstParams.Count == 0)
         return null;
     else
         return lstParams[0];
 }