首页 > 其他分享 >.net6 使用 Autofac

.net6 使用 Autofac

时间:2022-09-25 21:11:06浏览次数:49  
标签:Load Autofac 服务 Service builder 使用 net6 注入

  1. 在Nuget引入Autofac、Autofac.Extensions.DependencyInjection

  2. 定义Module,方便对注入服务进行管理

    public class AutoFacManager : Autofac.Module
        {
    
            //重写Autofac管道Load方法,在这里注册注入
            protected override void Load(ContainerBuilder builder)
            {
                //程序集注入业务服务
                var IAppServices = Assembly.Load("BaWei.RBAC.Repository");
                var AppServices = Assembly.Load("BaWei.RBAC.Service");
                //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖
     builder.RegisterAssemblyTypes(IAppServices).Where(t=>t.Name.EndsWith("Repository")).AsImplementedInterfaces();
     builder.RegisterAssemblyTypes(AppServices).Where(t=>t.Name.EndsWith("Service")).AsImplementedInterfaces();
            }
        }
    
  3. 在Program.cs中注册:

    //Autofac注入
    builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
    builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
    {
        builder.RegisterModule(new AutoFacManager());
    });
    
  4. 在构造函数中注入:

    IService _Service;
    public RoleController(IService Service)
    {
    	_Service = Service;
    }
    

标签:Load,Autofac,服务,Service,builder,使用,net6,注入
From: https://www.cnblogs.com/Coriander-Leo/p/16728932.html

相关文章

  • lambda函数和map函数的理解和使用
    lambda函数在说lambda函数前,先来想一下平时的在Python中怎么定义和使用函数的,简单的如下:defsum(x):x=x+5returnxprint(sum(8))输出结果都晓得:13上......
  • Linux pssh安装与使用
    Linuxpssh安装与使用说明:我这是没有在密钥认证的情况下操作1、安装pssh[root@libinansible]#yuminstall-ypssh[root@libinansible]#rpm-qlpssh/usr/bin/pnuk......
  • Docker使用问题汇总
    Docker使用问题汇总docker网段冲突因为疫情的原因居家办公,公司的阿里云数据库,通过外网可以访问,但是在容器里面访问不到。后经排查是因为docker容器的网段和公司的网段......
  • 在Linux下的文件IO的使用(一)
    系统调用系统调用:操作系统提供给用户程序调用的一组“特殊”接口,用户程序可以通过这组“特殊”接口来获得操作系统内核提供的服务为什么用户程序不能直接访问系......
  • 使用IOptionsSnapshot读取appsettings配置文件,将Json映射到对象
    {"Logging":{"LogLevel":{"Default":"Information","Microsoft.AspNetCore":"Warning"}},"AllowedHosts":"*","ConnectionStri......
  • git 使用 access token
    申请token在.git/config文件中修改修改前:[core] repositoryformatversion=0 filemode=false bare=false logallrefupdates=true symlinks=false ......
  • 使用Godaddy续费我的域名时遇到支付问题
    最近我的域名快到期了,需要跑到Godaddy上续费,因为听说续费时可以使用优惠码,我便在网上搜了一个优惠码PromoCode,试了一下的确是有效的也能便宜6元,然后便下单。结果在下单支......
  • 如何使用pandas中的时序数据分组运算
    https://www.zhihu.com/search?type=content&q=Pandas聚合时间序列数据注意以下的聚合是从当前时间点往后一段时间计算的。tmp_group=ori_data.groupby(['cols',pd.Gr......
  • vue使用轮播图插件vue-awesome-swiper
    案例,我目前使用的是vue2版本的2.6,安装vue-awesome-swiper是3.1.3版本的   第一步:在项目中使用npminstallvue-awesome-swiper--save 如果要指定我的版本......
  • ESLint的使用
    一.ESLint介绍目标了解ESLint的作用能判断ESLint的错误ESLint是什么ESLint是一个代码检查工具,用来检查你的代码是否符合指定的规范例如:=的前后必须有一个空格......