123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using Attribute;
- using Base;
- using Services;
- using System.Reflection;
- namespace Infrastructure
- {
- /// <summary>
- /// App服务注册
- /// </summary>
- public static class AppServiceExtensions
- {
- /// <summary>
- /// 注册引用程序域中所有有AppService标记的类的服务
- /// </summary>
- /// <param name="services"></param>
- public static void AddAppService(this IServiceCollection services)
- {
- // var cls = AppSettings.Get<string[]>("InjectClass");
- // if (cls == null || cls.Length <= 0)
- // {
- // throw new Exception("请更新appsettings类");
- // }
- // foreach (var item in cls)
- // {
- // Register(services, item);
- // }
- //必须注册的service
- // services.AddTransient<ISysOperLogService, SysOperLogService>();
- services.AddTransient<ISysPermissionService, SysPermissionService>();
- services.AddTransient<ISysDeptService, SysDeptService>();
- services.AddTransient<ISysLoginService, SysLoginService>();
- services.AddTransient<ISysMenuService, SysMenuService>();
- services.AddTransient<ISysRoleMenuService, SysRoleMenuService>();
- services.AddTransient<ISysPostService, SysPostService>();
- services.AddTransient<ISysRoleService, SysRoleService>();
- services.AddTransient<ISysUserPostService, SysUserPostService>();
- services.AddTransient<ISysUserRoleService, SysUserRoleService>();
- services.AddTransient<ISysRoleDeptService, SysRoleDeptService>();
- services.AddTransient<ISysUserService, SysUserService>();
- services.AddTransient<ISysOauthClientDetailsService, SysOauthClientDetailsService>();
- services.AddTransient<IApiGroupService, ApiGroupService>();
- services.AddTransient<IApiInfoService, ApiInfoService>();
- services.AddTransient<ISysDictService, SysDictService>();
- services.AddTransient<ISysDictItemService, SysDictItemService>();
- services.AddTransient<IPriProjectService, PriProjectService>();
- services.AddTransient<IPriProjectParamService, PriProjectParamService>();
- services.AddTransient<IPriQueryTableService, PriQueryTableService>();
- services.AddTransient<IPriQueryFieldService, PriQueryFieldService>();
- services.AddTransient<IPriReturnFieldService, PriReturnFieldService>();
- services.AddTransient<IPriListService, PriListService>();
- services.AddTransient<IPriListAmountSetService, PriListAmountSetService>();
- services.AddTransient<IPriConditionService, PriConditionService>();
- services.AddTransient<IPriLoopSetService, PriLoopSetService>();
- services.AddTransient<IPriRecordService, PriRecordService>();
- services.AddTransient<IPriLogService, PriLogService>();
- services.AddTransient<IPriDatabaseSetService, PriDatabaseSetService>();
- services.AddTransient<IPriPrizeObjectFieldService, PriPrizeObjectFieldService>();
- services.AddTransient<IPriPrizeObjectTableService, PriPrizeObjectTableService>();
- services.AddTransient<IPriRequestLogService, PriRequestLogService>();
- services.AddTransient<IPriRecursionStartFieldService, PriRecursionStartFieldService>();
- services.AddTransient<IPriRecursionStartTableService, PriRecursionStartTableService>();
- services.AddTransient<IPriRecursionStartReturnFieldService, PriRecursionStartReturnFieldService>();
- services.AddTransient<IPriRecursionStartConditionService, PriRecursionStartConditionService>();
- services.AddTransient<IPriRecursionEndFieldService, PriRecursionEndFieldService>();
- services.AddTransient<IPriRecursionEndTableService, PriRecursionEndTableService>();
- services.AddTransient<IPriPrizeInFieldService, PriPrizeInFieldService>();
- services.AddTransient<IPriPrizeInTableService, PriPrizeInTableService>();
- services.AddTransient<IPriPrizeInQueryFieldService, PriPrizeInQueryFieldService>();
- services.AddTransient<IPriPrizeInSetService, PriPrizeInSetService>();
- }
- private static void Register(IServiceCollection services, string item)
- {
- Assembly assembly = Assembly.Load(item);
- foreach (var type in assembly.GetTypes())
- {
- var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
- if (serviceAttribute != null)
- {
- var serviceType = serviceAttribute.ServiceType;
- //情况1 适用于依赖抽象编程,注意这里只获取第一个
- if (serviceType == null && serviceAttribute.InterfaceServiceType)
- {
- serviceType = type.GetInterfaces().FirstOrDefault();
- }
- //情况2 不常见特殊情况下才会指定ServiceType,写起来麻烦
- if (serviceType == null)
- {
- serviceType = type;
- }
- switch (serviceAttribute.ServiceLifetime)
- {
- case LifeTime.Singleton:
- services.AddSingleton(serviceType, type);
- break;
- case LifeTime.Scoped:
- services.AddScoped(serviceType, type);
- break;
- case LifeTime.Transient:
- services.AddTransient(serviceType, type);
- break;
- default:
- services.AddTransient(serviceType, type);
- break;
- }
- //System.Console.WriteLine($"注册:{serviceType}");
- }
- }
- }
- }
- }
|