Autofac 依赖注入小知识

控制反转/依赖注入 IOC/DI

依赖接口而不依赖于实现,是面向对象的六大设计原则(SOLID)之一。即依赖倒置原则(Dependence Inversion Principle)

生命周期分为三种,具体如下

  • Singleton 单例(全局唯一实例)
  • Scoped 范围 (在同一个生命周期内是同一个实例)
  • Transient 瞬时(每次请求都是一个新的实例)

使用说明

创建ASP.NET Core 3.0+的项目,并安装Autofac

dotnet add package Autofac.Extensions.DependencyInjection

在Program 中Host主机指定 .UseServiceProviderFactory(new AutofacServiceProviderFactory()).

UseServiceProviderFactory调用Autofac提供程序,附加到通用宿主机制。

public class Program
{
public static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
+ .UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webHostBuilder => {
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.Build(); host.Run();
}
}

在StartUp中配置

public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
} public IConfiguration Configuration { get; private set; } + public ILifetimeScope AutofacContainer { get; private set; } public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
} // ConfigureContainer is where you can register things directly
// with Autofac. This runs after ConfigureServices so the things
// here will override registrations made in ConfigureServices.
// Don't build the container; that gets done for you by the factory.
public void ConfigureContainer(ContainerBuilder builder)
{
// Register your own things directly with Autofac here. Don't
// call builder.Populate(), that happens in AutofacServiceProviderFactory
// for you.
+ builder.RegisterModule(new MyApplicationModule());
} public void Configure(
IApplicationBuilder app,
ILoggerFactory loggerFactory)
{
+ this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}

定义注入实现

public class MyApplicationModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
}
}
  • 注册泛型仓储
builder.RegisterGeneric(typeof(AuditBaseRepository<>)).As(typeof(IAuditBaseRepository<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(AuditBaseRepository<,>)).As(typeof(IAuditBaseRepository<,>)).InstancePerLifetimeScope();
  • 一个接口多个实现,使用Named,区分、参数为字符串即可。

注册服务

builder.RegisterType<IdentityServer4Service>().Named<ITokenService>(typeof(IdentityServer4Service).Name).InstancePerLifetimeScope();
builder.RegisterType<JwtTokenService>().Named<ITokenService>(typeof(JwtTokenService).Name).InstancePerLifetimeScope();

根据Name获取哪个服务

private readonly ITokenService _tokenService;
public AccountController(IComponentContext componentContext, IConfiguration configuration)
{
bool isIdentityServer4 = configuration.GetSection("Service:IdentityServer4").Value?.ToBoolean() ?? false;
_tokenService = componentContext.ResolveNamed<ITokenService>(isIdentityServer4 ? typeof(IdentityServer4Service).Name : typeof(JwtTokenService).Name);
}

可通过appsettings.json中配置,可决定是哪个服务

  "Service": {
"IdentityServer4": false
}
  • 基于接口的注入

AsImplementedInterfaces Specifies that a type from a scanned assembly is registered as providing all of its implemented interfaces.

指定将扫描程序集中的类型注册为提供其所有实现的接口。

根据接口ITransientDependency可以得到有哪些类继承了此接口,并判断是类,不是抽象类,不是泛型。

所有继承类接口的类,将以接口的方式自动注入实例。可直接使用接口即可。

  • InstancePerDependency 瞬时 (每次请求都是一个新的实例)
  • InstancePerLifetimeScope 范围(在同一个生命周期内是同一个实例)
  • SingleInstance 单例(全局唯一实例)
    public class DependencyModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
Assembly[] currentAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(r => r.FullName.Contains("LinCms.")).ToArray(); //每次调用,都会重新实例化对象;每次请求都创建一个新的对象;
Type transientDependency = typeof(ITransientDependency);
builder.RegisterAssemblyTypes(currentAssemblies)
.Where(t => transientDependency.GetTypeInfo().IsAssignableFrom(t) && t.IsClass && !t.IsAbstract && !t.IsGenericType)
.AsImplementedInterfaces().InstancePerDependency(); //同一个Lifetime生成的对象是同一个实例
Type scopeDependency = typeof(IScopedDependency);
builder.RegisterAssemblyTypes(currentAssemblies)
.Where(t => scopeDependency.GetTypeInfo().IsAssignableFrom(t) && t.IsClass && !t.IsAbstract && !t.IsGenericType)
.AsImplementedInterfaces().InstancePerLifetimeScope(); //单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;
Type singletonDependency = typeof(ISingletonDependency);
builder.RegisterAssemblyTypes(currentAssemblies)
.Where(t => singletonDependency.GetTypeInfo().IsAssignableFrom(t) && t.IsClass && !t.IsAbstract &&!t.IsGenericType)
.AsImplementedInterfaces().SingleInstance(); }
}

如果不写继承,如何批量注入呢。

1.类名有规则

2.基于特殊标签

3.继承接口。

  • 类名有规则

    比如仓储后缀,全是Repository,其中Assembly为仓储的实现所在程序集。将自动注入所有的仓储,仓储必须有接口。
    Assembly assemblysRepository = Assembly.Load("LinCms.Infrastructure");
builder.RegisterAssemblyTypes(assemblysRepository)
.Where(a => a.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
  • 注入服务后就执行一段逻辑
builder.RegisterType<MigrationStartupTask>().SingleInstance();
builder.RegisterBuildCallback(async (c) => await c.Resolve<MigrationStartupTask>().StartAsync());

动态代理

dotnet add package Autofac.Extras.DynamicProxy
dotnet add package Castle.Core.AsyncInterceptor
  • 服务注册

AOP+属性注入+以后缀为Service的服务实现,注入Scope 范围的生命周期+启用接口的拦截器。

  • 使用EnableInterfaceInterceptors创建执行拦截的接口代理,
  • 使用EnableClassInterceptors() 动态对子类进行重写, 执行virtual方法的拦截
builder.RegisterType<UnitOfWorkInterceptor>();
builder.RegisterType<UnitOfWorkAsyncInterceptor>(); List<Type> interceptorServiceTypes = new List<Type>()
{
typeof(UnitOfWorkInterceptor),
}; Assembly servicesDllFile = Assembly.Load("LinCms.Application");
builder.RegisterAssemblyTypes(servicesDllFile)
.Where(a => a.Name.EndsWith("Service") && !a.IsAbstract && !a.IsInterface && a.IsPublic)
.AsImplementedInterfaces()//接口注入
.InstancePerLifetimeScope()//生命周期:范围
.PropertiesAutowired()// 属性注入
.InterceptedBy(interceptorServiceTypes.ToArray())//声明拦截器
.EnableInterfaceInterceptors();//启用接口的拦截器。

这二个类,请参考如下代码

Autofac.Extras.DynamicProxy依赖Castle.Core,即只支持同步方法的拦截。

异步方法的拦截需要安装包:Castle.Core.AsyncInterceptor

  • 异步方法,分为有/无返回值:async Task RunAsync(),asyn Task<Result> RunAsync()
  • 同步方法:void Run(),Result Run()

同步拦截

1.定义拦截器

public class CallLogger : IInterceptor
{
TextWriter _output; public CallLogger(TextWriter output)
{
_output = output;
} public void Intercept(IInvocation invocation)
{
_output.Write("Calling method {0} with parameters {1}... ",
invocation.Method.Name,
string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); invocation.Proceed(); _output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}

2.注册拦截器。

// Named registration
builder.Register(c => new CallLogger(Console.Out))
.Named<IInterceptor>("log-calls"); // Typed registration
builder.Register(c => new CallLogger(Console.Out));

将拦截器与要拦截的类型 关联

[Intercept(typeof(CallLogger))]
public class First
{
public virtual int GetValue()
{
// Do some calculation and return a value
}
} // This attribute will look for a NAMED
// interceptor registration:
[Intercept("log-calls")]
public class Second
{
public virtual int GetValue()
{
// Do some calculation and return a value
}
}

链接

Autofac 依赖注入小知识的更多相关文章

  1. 从零开始,搭建博客系统MVC5+EF6搭建框架(2),测试添加数据、集成Autofac依赖注入

    一.测试仓储层.业务层是否能实现对数据库表的操作 1.创建IsysUserInfoRepository接口来继承IBaseRepository父接口 namespace Wchl.WMBlog.IRe ...

  2. 【干货】利用MVC5+EF6搭建博客系统(二)测试添加数据、集成Autofac依赖注入

    PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.测试仓储层.业务层是否能实现对数据库表的操作 1.在52MVCBlog.IRepository程序集下创建IsysUserInf ...

  3. asp.net mvc4 简单使用Autofac依赖注入小结

    1,首先使用 NuGet下载适当的Autofac版本 文件一,Autofac.3.5.2 文件二,Autofac.Mvc4.3.1.0 1,接口类 public interface IReposito ...

  4. ASP.NETCore使用AutoFac依赖注入

    原文:ASP.NETCore使用AutoFac依赖注入 实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using Sys ...

  5. ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下

    ADO.NET   一.ADO.NET概要 ADO.NET是.NET框架中的重要组件,主要用于完成C#应用程序访问数据库 二.ADO.NET的组成 ①System.Data  → DataTable, ...

  6. Autofac依赖注入

    简介 Autofac 是一款超赞的.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 .它的实现方式是将常规的.net类当做 组件 处理. ...

  7. Autofac依赖注入容器

    依赖注入容器-- Autofac https://github.com/danielpalme/IocPerformance Unity 更新频率高,微软的项目Grace 综合性能更高 目录: 一.简 ...

  8. Quartz使用AutoFac依赖注入问题小结

    theme: channing-cyan highlight: a11y-dark 背景 最近在做一个需求,就是在Job中捕捉异常,然后通过邮件或者消息的方式推送给指定人员,在需求实现的过程中遇到的一 ...

  9. Autofac 依赖注入

    介绍 Autofac是一款IOC框架,很轻量级性能非常高,自动注入很给力. NuGet Autofac:Autofac控制反转容器核心 Autofac.MVC5:提供IDependencyResolv ...

随机推荐

  1. 使用 CliWrap 让C#中的命令行交互举重若轻

    在代码中进行命令行交互是一个很常见的场景, 特别是在一些CI CD 自动化流程中, 在这之前我们会使用 System.Diagnostics.Process API, 现在有一个更灵活的工具 CliW ...

  2. [php安全]原生类的利用

    php原生类的利用 查看原生类中具有魔法函数的类 $classes = get_declared_classes(); foreach ($classes as $class) { $methods ...

  3. 100个Shell脚本——【脚本5】数字求和

    [脚本5]数字求和 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止,示例: 一.脚本 #!/bin/bash whi ...

  4. python做一个http接口测试框架

    目录结构 project case#测试用例 suite#测试目录 logs#测试日志 papi#测试类 result#测试结果 setting.py#配置文件 1.日志类,用于测试时日志记录 pya ...

  5. Linux学习 - 帮助命令

    一.获取帮助信息man(manual) 1 功能 获得命令或配置文件的帮助信息 2 语法 man  [1.5]  [命令或配置文件] 1 命令的帮助 (可用 whatis 代替) 5 配置文件的帮助 ...

  6. Android,iOS系统有什么区别

    两者运行机制不同:IOS采用的是沙盒运行机制,安卓采用的是虚拟机运行机制.Android是一种基于Linux的自由及开源的操作系统,iOS是由苹果公司开发的移动操作系统IOS中用于UI指令权限最高,安 ...

  7. entfrm-app赋能entfrm零代码开发平台 开启多平台分发

    entfrm-app是基于uni-app 框架.使用 Vue.js 语法开发的移动端 App开源产品.它可以编译为 H5.IOS App.Android App.微信小程序.QQ小程序.钉钉小程序.支 ...

  8. 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...

  9. 如何用shell脚本分析网站日志统计PV、404、500等数据

    以下shell脚本能统计出网站的总访问量,以及404,500出现的次数.统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击.还可以根据查看500出现的次数,进而判断网站 ...

  10. 【Linux】【Services】【Docker】应用

    1. Docker应用: 镜像:包含了启动Docker容器所需要的文件系统层级及其内容:基于UnionFS采用分层结构实现: bootfs,rootfs registry:保存docker镜像及镜像层 ...