ServiceConfiguration.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using YLShipBuildLandMap.Entity;
  2. using YLShipBuildLandMap.Services.SystemSetting;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.Loader;
  10. using System.Threading.Tasks;
  11. namespace YLShipBuildLandMap.Web
  12. {
  13. public static class ServiceConfiguration
  14. {
  15. public static void RegistLocalServices(IServiceCollection services)
  16. {
  17. List<Type> types = typeof(UserService).Assembly
  18. .GetTypes()
  19. .Where(x => !x.IsInterface && !x.IsAbstract && x.Name.Contains("Service"))
  20. .ToList();
  21. var extInterfaces = new List<Type>();
  22. Configuration.Assemblies.ForEach(assemblyName =>
  23. {
  24. AssemblyLoadContext.Default.LoadFromAssemblyPath(
  25. Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\" + assemblyName + ".dll");
  26. var extTypeList = Assembly.Load(assemblyName).GetTypes()
  27. .Where(x => !x.IsInterface && !x.IsAbstract && x.Name.Contains("Service")).ToList();
  28. extTypeList.ForEach(x =>
  29. x.GetInterfaces().ToList().ForEach(i =>
  30. services.AddScoped(i, x)
  31. )
  32. );
  33. extInterfaces.AddRange(extTypeList.SelectMany(x => x.GetInterfaces()).ToList());
  34. });
  35. foreach (var item in types)
  36. {
  37. var interfaceTypes = item.GetInterfaces();
  38. foreach (var interfaceType in interfaceTypes)
  39. {
  40. //跳过个性化配置涉及的类
  41. if (!extInterfaces.Any(x => x.FullName == interfaceType.FullName))
  42. {
  43. services.AddScoped(interfaceType, item);
  44. }
  45. }
  46. }
  47. //services.BuildServiceProvider();
  48. }
  49. }
  50. }