2023-06-01 18:47:11 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Infrastructure.Attribute;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
2023-03-18 07:58:39 +08:00
|
|
|
|
namespace ZR.Admin.WebApi.Extensions
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
2023-03-18 07:58:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// App服务注册
|
|
|
|
|
|
/// </summary>
|
2021-08-23 16:57:25 +08:00
|
|
|
|
public static class AppServiceExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册引用程序域中所有有AppService标记的类的服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
|
public static void AddAppService(this IServiceCollection services)
|
|
|
|
|
|
{
|
2023-06-01 18:47:11 +08:00
|
|
|
|
var cls = AppSettings.Get<string[]>("InjectClass");
|
|
|
|
|
|
if (cls == null || cls.Length <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("请更新appsettings类");
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
foreach (var item in cls)
|
|
|
|
|
|
{
|
2023-03-18 07:58:39 +08:00
|
|
|
|
Register(services, item);
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-18 07:58:39 +08:00
|
|
|
|
private static void Register(IServiceCollection services, string item)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
2023-03-18 07:58:39 +08:00
|
|
|
|
Assembly assembly = Assembly.Load(item);
|
2021-08-23 16:57:25 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2023-03-18 07:58:39 +08:00
|
|
|
|
//System.Console.WriteLine($"注册:{serviceType}");
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|