public void Add(IServiceCollection services, IConfiguration configuration) { services.AddScoped <Solution.ISolutionService, Solution.SolutionService>(); services.AddScoped <Solution.ISolutionComponentService, Solution.SolutionComponentService>(); services.AddScoped <Solution.ISolutionImporter, Solution.SolutionImporter>(); services.AddScoped <Solution.ISolutionExporter, Solution.SolutionExporter>(); //solution exporter var solutionComponentExporters = AssemblyHelper.GetClassOfType(typeof(Solution.Abstractions.ISolutionComponentExporter), "Xms.*.dll"); foreach (var exporter in solutionComponentExporters) { services.AddScoped(typeof(Solution.Abstractions.ISolutionComponentExporter), exporter); } //solution importer var solutionComponentImporters = AssemblyHelper.GetClassOfType(typeof(Solution.Abstractions.ISolutionComponentImporter <>), "Xms.*.dll"); foreach (var importer in solutionComponentImporters) { var it = importer.FindInterfaces((type, criteria) => { var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition()); return(isMatch); }, typeof(Solution.Abstractions.ISolutionComponentImporter <>)); foreach (var i in it) { services.AddScoped(i, importer); var nodeAttributes = importer.GetCustomAttributes(typeof(SolutionImportNodeAttribute), true); if (nodeAttributes != null && nodeAttributes.Length > 0) { ImporterNodeTypeMapper.Add(((SolutionImportNodeAttribute)nodeAttributes[0]).Name, importer); } else { throw new Exception($"class '{importer.Name}' not assigned 'SolutionImportNodeAttribute'"); } } } }
/// <summary> /// 导入解决方案 /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task <bool> ImportAsync(IFormFile file) { var dir = _webHelper.MapPath("~/solution/import/" + DateTime.Now.ToString("yyMMddhhmmss")); var d = Directory.CreateDirectory(dir); var filePath = dir + "/" + file.FileName; await file.SaveAs(filePath, _settingFinder, _webHelper).ConfigureAwait(false); IOHelper.UnZip(filePath); //解决方案信息 var solution = new Domain.Solution(); solution = solution.DeserializeFromXMLFile(dir + "/solution.xml"); var existSolution = _solutionService.FindById(solution.SolutionId); if (existSolution != null) { existSolution.Name = solution.Name; existSolution.Description = solution.Description; existSolution.Version = solution.Version; existSolution.ModifiedBy = _currentUser.SystemUserId; existSolution.ModifiedOn = DateTime.Now; _solutionService.Update(existSolution); } else { solution.CreatedBy = _currentUser.SystemUserId; solution.InstalledOn = DateTime.Now; solution.PublisherId = _currentUser.SystemUserId; _solutionService.Create(solution); } //自定义内容 XDocument doc = XDocument.Load(dir + "/customizations.xml"); IEnumerable <XElement> elements = from e in doc.Element("ImportExportXml").Elements() select e; foreach (var node in elements) { var importerTypeName = ImporterTypes[node.Name.ToString()]; if (importerTypeName.IsNotEmpty()) { var importerType = ImporterNodeTypeMapper.Get(node.Name.ToString());//Type.GetType(importerTypeName); if (importerType != null) { var componentArg = importerType.GetInterface(typeof(ISolutionComponentImporter <>).Name).GenericTypeArguments[0]; var importerService = _serviceResolver.Get(typeof(ISolutionComponentImporter <>).MakeGenericType(componentArg)); var listType = typeof(List <>).MakeGenericType(componentArg); var listObj = Activator.CreateInstance(listType); foreach (var item in node.Elements()) { var component = Activator.CreateInstance(componentArg); component = Serializer.FromXml(componentArg, item.ToString()); listType.GetMethod("Add").Invoke(listObj, new[] { component }); } //invoke import method var importMethod = importerType.GetMethod("Import"); var importResult = importMethod.Invoke(importerService, new[] { solution.SolutionId, listObj }); } } } //delete files Directory.Delete(dir, true); return(true); }