Пример #1
0
        /// <summary>
        /// 获取配置。
        /// </summary>
        /// <typeparam name="T">配置类型。</typeparam>
        /// <param name="getParameter">获取配置参数。</param>
        /// <param name="restartAppDomainOnChange">当配置改变时重新启动应用程序域。</param>
        /// <returns>配置。</returns>
        public override T GetMergedConfig <T>(IConfigParameter getParameter, bool restartAppDomainOnChange)
        {
            Check.Argument.IsNotNull("getParameter", getParameter);
            Check.Argument.IsAssignableFrom("getParameter", getParameter, typeof(XmlConfigGetParameter));

            T config = GetConfigFromCache <T>(getParameter);

            if (config == null)
            {
                lock (this.SyncLock)
                {
                    config = GetConfigFromCache <T>(getParameter);
                    if (config == null)
                    {
                        config = new T();

                        XmlConfigGetParameter cp = getParameter as XmlConfigGetParameter;
                        if (cp.HasFiles)
                        {
                            FileMergeResult mergeResult = new FileMergeResult(new List <string>(cp.Files));
                            foreach (string file in cp.Files)
                            {
                                try
                                {
                                    T splitConfig = Serializer.XmlSerializer.FromFile <T>(file);
                                    config.Merge(splitConfig);

                                    mergeResult.FilesMerged.Add(file);
                                }
                                catch (Exception ex)
                                {
                                    FileMergeFailReason mergeFailReasion = new FileMergeFailReason();
                                    mergeFailReasion.FileName        = file;
                                    mergeFailReasion.MergeFailReason = ExceptionHelper.GetMessage(ex);
                                    mergeResult.FileMergeFailReasons.Add(mergeFailReasion);
                                }
                            }

                            if (!mergeResult.HasFileMerged)
                            {
                                // Logger.Error
                            }
                            else if (!mergeResult.AllFilesMerged)
                            {
                                //Logger.Warning("Framework.Configuration", "XMLConfigProvider", fileMergeResult.ToString());
                            }

                            AddConfigToCache(getParameter, config);
                            SetupWacher(new XmlConfigChangeWatcher(cp));
                        }
                    }
                }
            }

            return(config);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public override T GetMergedConfig <T>()
        {
            ConfigFileAttribute attribute = AttributeHelper.GetConfigAttribute <ConfigFileAttribute>(typeof(T));

            if (attribute == null)
            {
                ConfigThrowHelper.ThrowConfigException(
                    R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(ConfigFileAttribute).FullName);
            }

            ConfigSettings        configSettings = FrameworkConfig.GetConfig <ConfigSettings>();
            string                configFilePath = configSettings.GetConfigFilePath <T>();
            XmlConfigGetParameter getParameter   = new XmlConfigGetParameter(configFilePath, attribute.IncludeSubdirectories);

            return(GetMergedConfig <T>(getParameter, attribute.RestartAppDomainOnChange));
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public override T GetConfig <T>()
        {
            ConfigFileAttribute attribute = AttributeHelper.GetConfigAttribute <ConfigFileAttribute>(typeof(T));

            if (attribute == null)
            {
                ConfigThrowHelper.ThrowConfigException(
                    R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(ConfigFileAttribute).FullName);
            }

            ConfigSettings configSettings = FrameworkConfig.GetConfig <ConfigSettings>();
            string         configFilePath = string.Empty;

            switch (attribute.ConfigPathType)
            {
            case ConfigPathType.FullPhysicalPath:
            {
                configFilePath = attribute.FileName;
            }
            break;

            case ConfigPathType.ServerPath:
                if (HttpContext.Current == null)
                {
                    configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, attribute.FileName.Replace("~/", ""));
                    break;
                }
                configFilePath = HttpContext.Current.Server.MapPath(attribute.FileName);
                break;

            default:
            {
                configFilePath = configSettings.GetConfigFilePath <T>();
            }
            break;
            }
            XmlConfigGetParameter getParameter = new XmlConfigGetParameter(configFilePath, attribute.IncludeSubdirectories);

            return(GetConfig <T>(getParameter, attribute.RestartAppDomainOnChange));
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override DateTime GetCurrentLastWriteTime()
        {
            DateTime lastWriteTime    = DateTime.MinValue;
            DateTime lastWriteTimeMax = DateTime.MinValue;

            XmlConfigGetParameter getParameter = this.GetParameter as XmlConfigGetParameter;

            string lastFilePaths = getParameter.FilePaths;

            getParameter.RefreshFiles();
            string currentFilePaths = getParameter.FilePaths;

            if (lastFilePaths == currentFilePaths)
            {
                foreach (string file in getParameter.Files)
                {
                    if (File.Exists(file))
                    {
                        lastWriteTime = File.GetLastWriteTime(file);
                        if (lastWriteTimeMax.CompareTo(lastWriteTime) == -1)
                        {
                            lastWriteTimeMax = lastWriteTime;
                        }
                    }
                    else
                    {
                        lastWriteTimeMax = DateTime.MaxValue;
                        break;
                    }
                }
            }
            else
            {
                lastWriteTimeMax = DateTime.MaxValue;
            }

            return(lastWriteTimeMax);
        }
 public XmlConfigChangeWatcher(XmlConfigGetParameter getParameter)
     : base(getParameter)
 {
 }
 public XmlConfigChangeWatcher(XmlConfigGetParameter getParameter)
     : base(getParameter)
 {
 }