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()); } } }
public static IConfigSetting Init(StartupSetting startupSetting, IConfigSetting appSetting) { var setting = startupSetting; var assemblies = new List <Assembly>(); //处理需要被明确加载的Assembly var assemblySettings = setting?.Assemblies; if (assemblySettings != null && assemblySettings.Length > 0) { foreach (var assembly in assemblySettings) { var dll = TypeHelper.AssemblyLoad(assembly.Name, false); if (dll != null && !assemblies.Contains(dll)) { assemblies.Add(dll); } } } //获取内嵌资源的文件格式 var resnamePatterns = new List <string> { ".devfx", ".devfx.config", ".devfxconfig", ".sqlconfig" }; var resourceSettings = setting?.ResourceFiles; if (resourceSettings != null && resourceSettings.Length > 0) { foreach (var resource in resourceSettings) { resnamePatterns.Add(resource.Name); } } var rootSetting = ConfigHelper.LoadConfigSettingFromAssemblies(new[] { typeof(ConfigServiceBase).Assembly }, assemblies, appSetting, setting?.ConfigSetting, resnamePatterns.ToArray()); setting = rootSetting.ToSetting <StartupSetting>($"{ConfigHelper.RootSettingName}/startup"); var fileSettings = setting?.ConfigFiles; if (fileSettings != null && fileSettings.Length > 0) { var configFiles = new List <string>(); foreach (var fileSetting in fileSettings) { var files = FileHelper.SearchFileWithPattern(fileSetting.Name, SearchOption.AllDirectories, AppDomain.CurrentDomain.BaseDirectory); if (files != null && files.Length > 0) { configFiles.AddRange(files); } } foreach (var fileName in configFiles) { var configSetting = ConfigHelper.CreateFromXmlFile(fileName); rootSetting.Merge(configSetting); } } ConfigSettingInherit(rootSetting[ConfigHelper.RootSettingName], new List <IConfigSetting>(), new List <IConfigSetting>()); var debugSetting = setting?.Debug; if (debugSetting != null && debugSetting.Enabled) { var fileName = debugSetting.FileName; if (!string.IsNullOrEmpty(fileName)) { fileName = FileHelper.GetPhysicalPath(".", fileName, true); File.WriteAllText(fileName, rootSetting.ToString()); } } return(rootSetting); }
/// <summary> /// 对Assembly进行依赖排序 /// </summary> /// <param name="list">排序后的结果</param> /// <param name="assemblies">需要进行排序的Assembly</param> /// <returns>排序后的结果</returns> internal static List <Assembly> SortAssemblies(List <Assembly> list, IEnumerable <Assembly> assemblies) { //根据程序集逻辑依赖进行排序 foreach (var assembly in assemblies) { if (!list.Contains(assembly)) { list.Add(assembly); } var index = list.IndexOf(assembly); var assemblyNames = assembly.GetReferencedAssemblies(); var referenced = new List <Assembly>(); foreach (var assemblyName in assemblyNames) { var ass = TypeHelper.AssemblyLoad(assemblyName, false); if (ass == null || !ass.IsDefined(typeof(ConfigResourceAttribute), false)) { continue; } var assIndex = list.IndexOf(ass); if (assIndex > index) { list.Remove(ass); } if (!list.Contains(ass) && !referenced.Contains(ass)) { referenced.Add(ass); } } if (referenced.Count > 0) { list.InsertRange(index, referenced); } } //根据程序集用户指定的依赖关系排序 var sortedList = new List <Assembly>(); for (var i = 0; i < list.Count;) { var assembly = list[i]; var hasDependency = false; if (sortedList.Contains(assembly)) //已经排序过了,直接跳过 { goto CONTINUE; } if (!assembly.IsDefined(typeof(ConfigDependencyAttribute), false)) //没有此属性,直接跳过 { goto CONTINUE; } var attributes = assembly.GetCustomAttributes(typeof(ConfigDependencyAttribute), false) as ConfigDependencyAttribute[]; if (attributes == null || attributes.Length <= 0) //没有此属性,直接跳过 { goto CONTINUE; } //获取依赖属性 var sortedAttributes = new List <ConfigDependencyAttribute>(attributes); //排序 sortedAttributes.Sort(CompareConfigDependencyAttributeByIndex); sortedAttributes.Reverse(); //把依赖程序集放到本程序集前 foreach (var dependencyAttribute in sortedAttributes) { var assName = dependencyAttribute.AssemblyName; if (string.IsNullOrEmpty(assName)) { continue; } var dependencyAssembly = TypeHelper.AssemblyLoad(assName, false); if (dependencyAssembly == null) { continue; } var dependencyIndex = list.IndexOf(dependencyAssembly); if (dependencyIndex <= i) //不存在或已经在本程序集前了,不处理 { continue; } //把依赖程序集放到本程序集前 list.Remove(dependencyAssembly); list.Insert(i, dependencyAssembly); hasDependency = true; } CONTINUE: if (!hasDependency) //没有依赖,则移动处理下一个 { i++; } if (!sortedList.Contains(assembly)) //把已处理过的标记下,下次就不用再处理了 { sortedList.Add(assembly); } } return(list); }