Webapi(.net6) 批量服务注册

news/2024/4/29 3:12:56

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

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

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

在这里插入图片描述
三个接口分别为:

public interface IScopedDependency
{
}
public interface ISingletonDependency
{
}
 public interface ITransientDependency{}

在实现类中使用如下:

 public interface IUserService{}public class UserService : IUserService, ITransientDependency{}

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

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);elseservices.AddSingleton(inter, type);}}}
}

在Programe下注册:

builder.Services.BatchAddServices("WebApi6");

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

2.1 新建个注入特性类:

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

使用方法如下:

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

2.2 扩展方法如下:

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));}}}
}
builder.Services.AddServices("WebApi6");

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10640.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

简易版 RPC 框架实现 1.0 -http实现

RPC 是“远程过程调用&#xff08;Remote Procedure Call&#xff09;”的缩写形式&#xff0c;比较通俗的解释是&#xff1a;像本地方法调用一样调用远程的服务。虽然 RPC 的定义非常简单&#xff0c;但是相对完整的、通用的 RPC 框架涉及很多方面的内容&#xff0c;例如注册发…

代码学习记录21--回溯算法第二天

随想录日记part21 t i m e &#xff1a; time&#xff1a; time&#xff1a; 2024.03.16 主要内容&#xff1a;今天主要是结合类型的题目加深对回溯算法的理解&#xff1a;1&#xff1a;组合总和&#xff1b;2&#xff1a;电话号码的字母组合 216.组合总和III17.电话号码的字母…

Linux系统——Session ID(负载均衡如何保持会话)

目录 一、实验环境搭建 二、部署Nginx代理服务器配置 三、部署后端真是服务器Tomcat配置 四、配置Tomcat的Session ID会话保持 五、测试 此次实验是Tomcat后端服务器如何做Session ID会话保持 一、实验环境搭建 [rootlocalhost ~]#systemctl stop firewalld [rootlocalho…

用云服务器构建gpt和stable-diffusion大模型

用云服务器构建gpt和stable-diffusion大模型 一、前置知识二、用云端属于自己的聊天chatGLM3step1、项目配置step2、环境配置1、前置知识2、环境配置流程 step3、创建镜像1、前置知识2、创建镜像流程 step4、通过 Gradio 创建ChatGLM交互界面1、前置知识2、创建ChatGLM交互界面…

【Vue3】深入理解Vue3路由器的工作原理to的两种写法

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

Vue3 ElementPlus-table组件(合计)合并列

在使用ElementPlus的table组件的时候&#xff0c;我们通常会处理合计&#xff0c;当遇到合计行需要合并列的时候&#xff0c;可以这样做。 核心就是获取标签&#xff0c;对标签的CSS样式进行设置&#xff0c;以达到合并单元格的效果。 Template <el-tablemax-height"ca…