AssemblyInjectConfigure.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Bmp.Core.Common.AssemblyInject.Interfaces;
  2. using Bmp.Core.Common.AssemblyInject.Services;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Logging;
  5. namespace Bmp.Core.Common.AssemblyInject;
  6. internal class AssemblyInject { }
  7. public static class AssemblyInjectConfigure
  8. {
  9. public static void AddAssemblyInject<T>(this IServiceCollection services)
  10. {
  11. var aiLogger = services.BuildServiceProvider().GetService<ILogger<AssemblyInject>>();
  12. aiLogger?.LogInformation("Discovering components...");
  13. var componentsAdded = 0;
  14. //单例组件
  15. var siLogger = services.BuildServiceProvider().GetService<ILogger<IAssemblyInjectSingleton>>();
  16. foreach (var svcType in typeof(T).Assembly.GetTypes()
  17. .Where(typeof(IAssemblyInjectSingleton).IsAssignableFrom)
  18. .Where(p => p.IsInterface == false && p.IsAbstract == false)
  19. .OrderBy(p => p.Name))
  20. {
  21. var gsi = svcType.GetInterfaces().Where(p => p.IsGenericType && p.GetGenericTypeDefinition() == typeof(IAssemblyInjectSingleton<>)).ToArray();
  22. if (gsi.Length == 0)
  23. {
  24. siLogger?.LogInformation($"AddSingleton {svcType.FullName}");
  25. services.AddSingleton(svcType);
  26. ++componentsAdded;
  27. }
  28. else
  29. {
  30. foreach (var type in gsi)
  31. {
  32. var addAs = type.GetGenericArguments().First()!;
  33. siLogger?.LogInformation($"AddSingleton {svcType.FullName} as {addAs.FullName}");
  34. services.AddSingleton(addAs, svcType);
  35. ++componentsAdded;
  36. }
  37. }
  38. }
  39. //Transient组件
  40. var tsLogger = services.BuildServiceProvider().GetService<ILogger<IAssemblyInjectTransient>>();
  41. foreach (var svcType in typeof(T).Assembly.GetTypes()
  42. .Where(typeof(IAssemblyInjectTransient).IsAssignableFrom)
  43. .Where(p => p.IsInterface == false && p.IsAbstract == false))
  44. {
  45. var gsi = svcType.GetInterfaces().Where(p => p.IsGenericType && p.GetGenericTypeDefinition() == typeof(IAssemblyInjectTransient<>)).ToArray();
  46. if (gsi.Length == 0)
  47. {
  48. tsLogger?.LogInformation($"AddTransient {svcType.FullName}");
  49. services.AddTransient(svcType);
  50. ++componentsAdded;
  51. }
  52. else
  53. {
  54. foreach (var type in gsi)
  55. {
  56. var addAs = type.GetGenericArguments().First()!;
  57. siLogger?.LogInformation($"AddTransient {svcType.FullName} as {addAs.FullName}");
  58. services.AddTransient(addAs, svcType);
  59. ++componentsAdded;
  60. }
  61. }
  62. }
  63. if (componentsAdded == 0)
  64. {
  65. var logger = services.BuildServiceProvider().GetService<ILogger<AssemblyInjectSyncStartStopWorker<T>>>();
  66. logger?.LogWarning("Skip: No found any AssemblyInject components");
  67. }
  68. else
  69. {
  70. aiLogger?.LogInformation($"Discovered number of components:{componentsAdded}");
  71. //自动启停Worker
  72. services.AddHostedService<AssemblyInjectSyncStartStopWorker<T>>();
  73. }
  74. }
  75. }