1. 什么情况下需要引入第三方容器组件
- 基于名称的注入
需要把一个服务按照名称来区分它不同的实现的时候,
- 属性注入
1. 之前注入方式有FromService的方式,还有构造函数入参的方式
2. 属性注入就是直接把服务注册到某一个类的属性里面去,而不需要定义构造函数
- 子容器
- 基于动态代理的AOP
需要在服务中注入我们额外的行为的时候,可以用动态代理的能力。
2. Nuget包
- Autofac.Extensions.DependencyInjection
- Autofac.Extras.DynamicProxy
3. 修改代码,注入第三方容器
改动1:Program.cs -> CreateHostBuilder
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) //新增代码
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel().UseUrls("http://*:8001").UseStartup<Startup>();
});
改动2:StartUp.cs 新增 ConfigureContainer方法
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
}
4. 基本用法
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<MyService>().As<IMyService>();
}
//注册根容器
public ILifetimeScope AutofacContainer { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
var serviceName = this.AutofacContainer.Resolve<IMyService>();
serviceName.ShowCode();
//...
}
5. 命名注册
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
//需要把一个服务注册多次,并且用不同的命名来做区分的时候
builder.RegisterType<MyServiceV2>().Named<IMyService>("service2");
}
/// <summary>
/// 注册根容器
/// </summary>
public ILifetimeScope AutofacContainer { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
var service = this.AutofacContainer.ResolveNamed<IMyService>("service2");
service.ShowCode();
//...
}
6. 属性注册
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<MyNameService>();
builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired();
}
/// <summary>
/// 注册根容器
/// </summary>
public ILifetimeScope AutofacContainer { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
var serviceName = this.AutofacContainer.Resolve<IMyService>();
serviceName.ShowCode();
//...
}
7. AOP
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
//AOP主要应用场景是指我们在不期望改变原有类的情况下,在方法执行时嵌入一些逻辑,
//让我们可以在方法执行的切面上任意的插入我们的逻辑
//builder.RegisterType<MyInterceptor>(); //拦截器注册到容器种
//builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired().InterceptedBy(typeof(MyInterceptor)).EnableInterfaceInterceptors();
}
/// <summary>
/// 注册根容器
/// </summary>
public ILifetimeScope AutofacContainer { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
var serviceName = this.AutofacContainer.Resolve<IMyService>();
serviceName.ShowCode();
//...
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("执行AOP方法之前");
invocation.Proceed();
Console.WriteLine("执行AOP方法之后");
}
}
8. 子容器
/// <summary>
/// ConfigureServices将默认的容器注入进去,Autofac会接替默认的,然后执行ConfigureContainer
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
//把一个服务注入到特定命名的子容器中,其他子容器获取不到这个对象
builder.RegisterType<MyNameService>().InstancePerMatchingLifetimeScope("myscope");
}
/// <summary>
/// 注册根容器
/// </summary>
public ILifetimeScope AutofacContainer { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
using (var myscope = AutofacContainer.BeginLifetimeScope("myscope"))
{
var service0 = myscope.Resolve<MyNameService>();
using(var scope = myscope.BeginLifetimeScope())
{
var service1 = scope.Resolve<MyNameService>();
var service2 = scope.Resolve<MyNameService>();
Console.WriteLine($"service1=service2:{service1 == service2}");
Console.WriteLine($"service0=service2:{service1 == service0}");
}
}
//..
}