using CloudBuilder.Core.Application.Builders; using CloudBuilder.Core.Application.Packs; using CloudBuilder.Core.Extensions; using CloudBuilder.Core.Service; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace CloudBuilder.Core.DependencyInjection.Service { [PackLevel(20)] public class ServicePack : CloudBuilderPack { /// /// 获取 模块级别 /// public override PackLevel Level => PackLevel.Business; /// /// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动 /// public override int Order => 2; /// /// 将服务添加到依赖注入服务容器中 /// /// 依赖注入服务容器 /// public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder) { var baseType = typeof(IService); var allAssemblies = CloudBuilderBuilder.GetAssemblies(); var types = allAssemblies .SelectMany(a => a.DefinedTypes) .Select(type => type.AsType()) .Where(type => type != baseType && type.HasImplementedRawGeneric(baseType)) .Where(x => x.IsClass).ToList(); foreach (var implementType in types) { services.TryAddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType); } // IAuthService authRequest = ServiceProvider.GetService()!; return services; } } }