Webapi(.net6) 批量服务注册

如果不考虑第三方库,如Autofac这种进行服务注入,通过本身的.Core Weabpi实现的,总结了两种实现方法,

1.一种是参考abp框架里面的形式;

1.1 新建个生命周期的文件夹:

三个接口分别为:

csharp 复制代码
public interface IScopedDependency
{
}
csharp 复制代码
public interface ISingletonDependency
{
}
csharp 复制代码
 public interface ITransientDependency
 {
 }

在实现类中使用如下:

csharp 复制代码
 public interface IUserService
 {
 }

 public class UserService : IUserService, ITransientDependency
 {
 }

1.2 建立个批量服务注册的扩展方法:

csharp 复制代码
public static class ConfigSservice
{
    public static void BatchAddServices(this IServiceCollection services, string assemblyString)
    {
        var assembly = Assembly.Load(assemblyString);
        var types = assembly.GetTypes();
        var list = types.Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType
        && (typeof(ITransientDependency).IsAssignableFrom(u)
        || typeof(IScopedDependency).IsAssignableFrom(u)
        || typeof(ISingletonDependency).IsAssignableFrom(u))).ToList();
        foreach (var type in list)
        {
            var interfaceList = type.GetInterfaces();
            if (interfaceList.Any())
            {
                var inter = interfaceList.First();
                if (typeof(ITransientDependency).IsAssignableFrom(type))
                    services.AddTransient(inter, type);
                else if (typeof(IScopedDependency).IsAssignableFrom(type))
                    services.AddScoped(inter, type);
                else
                    services.AddSingleton(inter, type);
            }
        }
    }
}

在Programe下注册:

csharp 复制代码
builder.Services.BatchAddServices("WebApi6");

2.第二种通过特性方法:

2.1 新建个注入特性类:

csharp 复制代码
 [AttributeUsage(AttributeTargets.Class, Inherited = false,AllowMultiple = false)]
 public class InjectServiceAttribute : Attribute
 {
     public Type ServiceType { get; set; }
     public ServiceLifetime Lifetime { get; set; }
 }

使用方法如下:

csharp 复制代码
  public interface IUserService
  {
  }

  [InjectService(Lifetime = ServiceLifetime.Transient, ServiceType = typeof(IUserService))]
  public class UserService
  {
  }

2.2 扩展方法如下:

csharp 复制代码
public static class DependencyInjection
{
    public static void AddServices(this IServiceCollection services, string assemblyName)
    {
        var assembly = Assembly.Load(assemblyName);
        if (assembly != null)
        {
            var types = assembly.GetTypes().Where(s => s.IsClass && !s.IsAbstract).ToList();
            foreach (var type in types)
            {
                var injectService = type.GetCustomAttribute<InjectServiceAttribute>();
                if (injectService == null)
                    continue;

                var serviceType = injectService.ServiceType ?? type;
                var implementationType = type;
                var lifetime = injectService.Lifetime;
                services.Add(new ServiceDescriptor(serviceType, implementationType, lifetime));
            }
        }
    }
}
csharp 复制代码
builder.Services.AddServices("WebApi6");

如有不对或者建议,欢迎提出指正!

相关推荐
蒙塔基的钢蛋儿6 小时前
告别内存泄露与空指针:用C#与.NET 10开启STM32H7高性能单片机开发新纪元
stm32·c#·.net
时光追逐者7 小时前
一款基于 .NET 开源、跨平台应用程序自动升级组件
c#·.net·.net core
海盗12348 小时前
ScottPlot在WPF的基本使用和中文乱码问题
c#·.net·wpf
我是唐青枫9 小时前
C#.NET System.Threading.Lock 深入解析:新一代 lock、底层语义与使用边界
c#·.net
步步为营DotNet10 小时前
基于.NET 11 与C# 14的高性能安全客户端应用开发
安全·c#·.net
毕设源码-邱学长1 天前
【开题答辩全过程】以 基于.net mvc剧本杀预约与管理为例,包含答辩的问题和答案
mvc·.net
似水明俊德1 天前
12-C#.Net-加密解密-学习笔记
笔记·学习·oracle·c#·.net
阿蒙Amon1 天前
C#常用类库-详解SSH.NET
c#·ssh·.net
似水明俊德1 天前
11-C#.Net-多线程-Async-Await篇-学习笔记
开发语言·笔记·学习·c#·.net