CloudBuilder/CloudBuilder.Core/DependencyInjection/Service/ServicePack.cs
owenchen 1c0c83af5f ow
2026-05-12 10:09:31 +08:00

54 lines
1.7 KiB
C#

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
{
/// <summary>
/// 获取 模块级别
/// </summary>
public override PackLevel Level => PackLevel.Business;
/// <summary>
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
/// </summary>
public override int Order => 2;
/// <summary>
/// 将服务添加到依赖注入服务容器中
/// </summary>
/// <param name="services">依赖注入服务容器</param>
/// <returns></returns>
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<IAuthService>()!;
return services;
}
}
}