using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Services.SystemSetting; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Loader; using System.Threading.Tasks; namespace YLShipBuildLandMap.Web { public static class ServiceConfiguration { public static void RegistLocalServices(IServiceCollection services) { List types = typeof(UserService).Assembly .GetTypes() .Where(x => !x.IsInterface && !x.IsAbstract && x.Name.Contains("Service")) .ToList(); var extInterfaces = new List(); Configuration.Assemblies.ForEach(assemblyName => { AssemblyLoadContext.Default.LoadFromAssemblyPath( Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\" + assemblyName + ".dll"); var extTypeList = Assembly.Load(assemblyName).GetTypes() .Where(x => !x.IsInterface && !x.IsAbstract && x.Name.Contains("Service")).ToList(); extTypeList.ForEach(x => x.GetInterfaces().ToList().ForEach(i => services.AddScoped(i, x) ) ); extInterfaces.AddRange(extTypeList.SelectMany(x => x.GetInterfaces()).ToList()); }); foreach (var item in types) { var interfaceTypes = item.GetInterfaces(); foreach (var interfaceType in interfaceTypes) { //跳过个性化配置涉及的类 if (!extInterfaces.Any(x => x.FullName == interfaceType.FullName)) { services.AddScoped(interfaceType, item); } } } //services.BuildServiceProvider(); } } }