ServiceCollectionExtensions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using System;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace FastGithub
  7. {
  8. /// <summary>
  9. /// 服务注册扩展
  10. /// </summary>
  11. public static class ServiceCollectionExtensions
  12. {
  13. /// <summary>
  14. /// 注册程序集下所有服务下选项
  15. /// </summary>
  16. /// <param name="services"></param>
  17. /// <param name="configuration">配置</param>
  18. /// <returns></returns>
  19. public static IServiceCollection AddServiceAndOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
  20. {
  21. services.AddAttributeServices(assembly);
  22. services.AddAttributeOptions(assembly, configuration);
  23. return services;
  24. }
  25. /// <summary>
  26. /// 添加程序集下ServiceAttribute标记的服务
  27. /// </summary>
  28. /// <param name="services"></param>
  29. /// <param name="assembly"></param>
  30. /// <returns></returns>
  31. private static IServiceCollection AddAttributeServices(this IServiceCollection services, Assembly assembly)
  32. {
  33. var implTypes = assembly
  34. .GetTypes()
  35. .Where(item => item.IsClass && item.IsAbstract == false)
  36. .ToArray();
  37. foreach (var implType in implTypes)
  38. {
  39. var attributes = implType.GetCustomAttributes<ServiceAttribute>(false);
  40. foreach (var attr in attributes)
  41. {
  42. var serviceType = attr.ServiceType ?? implType;
  43. if (services.Any(item => item.ServiceType == serviceType && item.ImplementationType == implType) == false)
  44. {
  45. var descriptor = ServiceDescriptor.Describe(serviceType, implType, attr.Lifetime);
  46. services.Add(descriptor);
  47. }
  48. }
  49. }
  50. return services;
  51. }
  52. /// <summary>
  53. /// 添加程序集下OptionsAttribute标记的服务
  54. /// </summary>
  55. /// <param name="services"></param>
  56. /// <param name="assembly"></param>
  57. /// <param name="configuration"></param>
  58. private static IServiceCollection AddAttributeOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
  59. {
  60. foreach (var optionsType in assembly.GetTypes())
  61. {
  62. var optionsAttribute = optionsType.GetCustomAttribute<OptionsAttribute>();
  63. if (optionsAttribute != null)
  64. {
  65. var key = optionsAttribute.SessionKey ?? optionsType.Name;
  66. var section = configuration.GetSection(key);
  67. OptionsBinder.Create(services, optionsType).Bind(section);
  68. }
  69. }
  70. return services;
  71. }
  72. /// <summary>
  73. /// options绑定器
  74. /// </summary>
  75. private abstract class OptionsBinder
  76. {
  77. public abstract void Bind(IConfiguration configuration);
  78. /// <summary>
  79. /// 创建OptionsBinder实例
  80. /// </summary>
  81. /// <param name="services"></param>
  82. /// <param name="optionsType"></param>
  83. /// <returns></returns>
  84. public static OptionsBinder Create(IServiceCollection services, Type optionsType)
  85. {
  86. var binderType = typeof(OptionsBinderImpl<>).MakeGenericType(optionsType);
  87. var binder = Activator.CreateInstance(binderType, new object[] { services });
  88. return binder is OptionsBinder optionsBinder
  89. ? optionsBinder
  90. : throw new TypeInitializationException(binderType.FullName, null);
  91. }
  92. private class OptionsBinderImpl<TOptions> : OptionsBinder where TOptions : class
  93. {
  94. private readonly IServiceCollection services;
  95. public OptionsBinderImpl(IServiceCollection services)
  96. {
  97. this.services = services;
  98. }
  99. public override void Bind(IConfiguration configuration)
  100. {
  101. this.services.AddOptions<TOptions>().Bind(configuration);
  102. }
  103. }
  104. }
  105. }
  106. }