self_mall_api/Infrastructure/Extensions/AppServiceExtensions.cs
2023-06-02 21:15:33 +08:00

72 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Reflection;
namespace Infrastructure.Extensions
{
public static class AppServiceExtensions
{
/// <summary>
/// 注册引用程序域中所有有AppService标记的类的服务
/// </summary>
/// <param name="services"></param>
public static void AddAppService(this IServiceCollection services)
{
//var assemblies = AppDomain.CurrentDomain.GetAssemblies();
string []cls = new string[] { "ARW.Repository", "ARW.Service", "ARW.Tasks" };
foreach (var item in cls)
{
Assembly assembly = Assembly.Load(item);
Register(services, assembly);
}
}
private static void Register(IServiceCollection services, Assembly assembly)
{
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;
}
//Console.WriteLine($"注册:{serviceType}");
}
else
{
//Console.WriteLine($"注册:{serviceType}");
}
}
}
}
}