Autofac生命周期
作者:互联网
Autofac注入方式
前面一节介绍了Autofac注入方式,这节介绍Autofac的生命周期,实例生命周期决定在同一个服务的每个请求的实例是如何共享的。
环境
Win10+VS2020+.NET 5.0 +Autofac 6.3.0
Autofac几种生命周期
l InstancePerDependency
l SingleInstance
l InstancePerLifetimeScope
l InstancePerMatchingLifetimeScope
l InstancePerOwned
l InstancePerHttpRequest
实践
项目结构
在上节Autofac的快速入门的项目上改造。
InstancePerDependency
默认模式,瞬时生命周期,每次调用,都会重新实例化对象;每次请求都创建一个新的对象;
var builder = new ContainerBuilder(); builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerDependency(); var container = builder.Build(); var testServiceA = container.Resolve<ITestServiceA>(); var testServiceA1 = container.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1)); |
运行结果:
SingleInstance
单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;
var builder = new ContainerBuilder(); builder.RegisterType<TestServiceA>().As<ITestServiceA>().SingleInstance(); var container = builder.Build(); var testServiceA = container.Resolve<ITestServiceA>(); var testServiceA1 = container.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1));
|
InstancePerLifetimeScope
同一个生命周期域中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;且每个不同的生命周期域中的实例是唯一的,不共享的。
var builder = new ContainerBuilder(); builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerLifetimeScope(); var container = builder.Build(); ITestServiceA testServiceA15; ITestServiceA testServiceA16; using (var scope1 = container.BeginLifetimeScope()) { var testServiceA11 = scope1.Resolve<ITestServiceA>(); var testServiceA12 = scope1.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA11,testServiceA12)); testServiceA15 = testServiceA12; } using (var scope1 = container.BeginLifetimeScope()) { var testServiceA13 = scope1.Resolve<ITestServiceA>(); var testServiceA14 = scope1.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA13,testServiceA14)); testServiceA16 = testServiceA14; } Console.WriteLine(object.ReferenceEquals(testServiceA15,testServiceA16));
|
InstancePerMatchingLifetimeScope
同一个匹配的生命周期域中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;且每个不匹配的生命周期域中的实例是唯一的,不共享的。
ContainerBuilder containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerMatchingLifetimeScope("Yak");
IContainer container = containerBuilder.Build();
ITestServiceA testServiceA15 = null; ITestServiceA testServiceA16 = null; using (var scope1 = container.BeginLifetimeScope("Yak")) { ITestServiceA testServiceA11 = scope1.Resolve<ITestServiceA>(); using (var scope2 = scope1.BeginLifetimeScope()) { ITestServiceA testServiceA12 = scope2.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA11, testServiceA12)); } testServiceA15 = testServiceA11; }
using (var scope1 = container.BeginLifetimeScope("Yak")) { ITestServiceA testServiceA13 = scope1.Resolve<ITestServiceA>(); using (var scope2 = scope1.BeginLifetimeScope()) { ITestServiceA testServiceA14 = scope2.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(testServiceA13, testServiceA14)); }
testServiceA16 = testServiceA13; } Console.WriteLine(object.ReferenceEquals(testServiceA15, testServiceA16));
|
InstancePerOwned
在一个所拥有的实例创建的生命周期中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;(较少使用)
InstancePerHttpRequest
同一次Http请求上下文中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;仅适用于 ASP.NET (CORE) MVC 或 WebForm 应用程序
总结
Autofac可以返回一个单例 (SingleInstance ), 一个新的对象 (InstancePerLifetimeScope ) 或者在某种上下文环境中的单例。比如 一个线程 或者一个HTTP请求 (InstancePerHttpRequest )。
标签:Autofac,生命周期,container,实例,builder,Resolve,scope1,var 来源: https://www.cnblogs.com/yakniu/p/16273301.html