示例#1
0
        protected virtual void OnInit(IStartupSetting startupSetting)
        {
            var appSetting = ConfigSectionHandler.GetConfig("htb.devfx");
            var setting    = startupSetting ?? appSetting.ToSetting <StartupSetting>("startup");
            var assemblies = new List <Assembly>();

            if (setting != null && setting.CoreSetting != null && setting.CoreSetting.ConfigServiceSetting != null && setting.CoreSetting.ConfigServiceSetting.Assemblies != null)
            {
                var assemblySettings = setting.CoreSetting.ConfigServiceSetting.Assemblies;
                foreach (var assembly in assemblySettings)
                {
                    var dll = TypeHelper.AssemblyLoad(assembly.AssemblyName, false);
                    if (dll != null && !assemblies.Contains(dll))
                    {
                        assemblies.Add(dll);
                    }
                }
            }

            var rootSetting = ConfigHelper.LoadConfigSettingFromAssemblies(new[] { typeof(Esb.IServiceLocator).Assembly, typeof(ConfigService).Assembly }, assemblies, appSetting, setting);

            setting = rootSetting.ToSetting <StartupSetting>("htb.devfx/startup");
            IConfigServiceSetting configServiceSetting = null;

            if (setting != null && setting.CoreSetting != null && setting.CoreSetting.ConfigServiceSetting != null)
            {
                configServiceSetting = setting.CoreSetting.ConfigServiceSetting;
                if (setting.CoreSetting.ConfigServiceSetting.ConfigFiles != null && setting.CoreSetting.ConfigServiceSetting.ConfigFiles.Length > 0)
                {
                    var configFiles = new List <string>();
                    foreach (var fileSetting in setting.CoreSetting.ConfigServiceSetting.ConfigFiles)
                    {
                        var files = FileHelper.SearchFileWithPattern(fileSetting.FileName, SearchOption.AllDirectories, AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
                        if (files != null && files.Length > 0)
                        {
                            configFiles.AddRange(files);
                        }
                    }
                    foreach (var fileName in configFiles)
                    {
                        var configSetting = ConfigHelper.CreateFromXmlFile(fileName);
                        rootSetting.Merge(configSetting);
                    }
                }
            }

            ConfigSettingInherit(rootSetting["htb.devfx"], new List <IConfigSetting>(), new List <IConfigSetting>());
            this.Settings = rootSetting;
            if (configServiceSetting != null && configServiceSetting.Debug != null && configServiceSetting.Debug.Enabled)
            {
                var outputFileName = configServiceSetting.Debug.FileName;
                if (!string.IsNullOrEmpty(outputFileName))
                {
                    outputFileName = FileHelper.GetPhysicalPath(".", outputFileName, true);
                    File.WriteAllText(outputFileName, rootSetting.ToString());
                }
            }
        }
示例#2
0
        internal static IConfigSetting LoadConfigSettingFromAssemblies(Assembly[] coreAssemblies, List <Assembly> assemblies, IConfigSetting appSetting, IConfigSettingRequired setting)
        {
            if (appSetting == null)
            {
                appSetting = ConfigSectionHandler.GetConfig("htb.devfx");
            }

            if (assemblies == null)
            {
                assemblies = new List <Assembly>();
            }

            assemblies = SortAssemblies(assemblies, TypeHelper.GetAssembliesByCustomAttribute(typeof(ConfigResourceAttribute)));
            for (var i = coreAssemblies.Length - 1; i >= 0; i--)
            {
                var coreAssembly = coreAssemblies[i];
                if (assemblies.IndexOf(coreAssembly) != 0)
                {
                    assemblies.Remove(coreAssembly);
                    assemblies.Insert(0, coreAssembly);
                }
            }

            var resources = new List <ConfigResourceAttribute>();

            foreach (var assembly in assemblies)
            {
                var attributes = assembly.GetCustomAttributes(typeof(ConfigResourceAttribute), true) as ConfigResourceAttribute[];
                if (attributes != null && attributes.Length > 0)
                {
                    foreach (var attribute in attributes)
                    {
                        attribute.Assembly = assembly;
                    }
                    var attributeList = new List <ConfigResourceAttribute>(attributes);
                    attributeList.Sort(CompareConfigResourceAttributeByIndex);
                    resources.AddRange(attributeList);
                }
            }

            var rootSetting = CreateFromXmlString("<configuration><htb.devfx /></configuration>");

            foreach (var resource in resources)
            {
                var configSetting = CreateFromXmlSource(resource.Resource, resource.Assembly);
                rootSetting.Merge(configSetting);
            }

            IConfigSetting userRootSetting = null;

            if (setting != null && setting.ConfigSetting != null)
            {
                userRootSetting = setting.ConfigSetting.GetRootSetting();
            }
            if (userRootSetting == null)
            {
                userRootSetting = appSetting;
            }

            if (userRootSetting != null)
            {
                var mergeSetting = rootSetting;
                if (userRootSetting.SettingName == "htb.devfx")
                {
                    mergeSetting = rootSetting["htb.devfx"];
                }
                if (mergeSetting != null)
                {
                    mergeSetting.Merge(userRootSetting);
                }
            }
            return(rootSetting);
        }