山西经贸职业学院,城步县,计算机科学与技术属于什么类
core提供了默认的依赖注入容器,可以在startup.configureservices方法中进行服务注入的配置。
默认的依赖注入容器提供了三种生命周期:
下面试验一下这三种方式的差异:
注入配置:
services.addsingleton<isingletontest, singletontest>(); services.addtransient<itransienttest, transienttest>(); services.addscoped<iscopedtest, scopedtest>(); services.addtransient<scopeandtransienttester>();
控制器:
public defaultcontroller(isingletontest singleton, itransienttest transient, iscopedtest scoped, scopeandtransienttester scopeandtransienttester ) { this.singleton = singleton; this.transient = transient; this.scoped = scoped; this.scopeandtransienttester = scopeandtransienttester; } [httpget] public string get() { return $"singleton={singleton.guid}; \r\nscoped1={scoped.guid};\r\nscoped2={scopeandtransienttester.scopedid()};\r\ntransient={transient.guid};\r\ntransient2={scopeandtransienttester.transientid()};"; }
用于第二次注入的scopeandtransienttester类:
public class scopeandtransienttester { public isingletontest singleton { get; } public itransienttest transient { get; } public iscopedtest scoped { get; } public scopeandtransienttester(isingletontest singleton, itransienttest transient, iscopedtest scoped) { this.singleton = singleton; this.transient = transient; this.scoped = scoped; } public guid singletonid() { return singleton.guid; } public guid transientid() { return transient.guid; } public guid scopedid() { return scoped.guid; } }
第一次请求:
singleton=ebece97f-bd38-431c-9fa0-d8af0419dcff; scoped1=426eb574-8f34-4bd3-80b3-c62366fd4c74; scoped2=426eb574-8f34-4bd3-80b3-c62366fd4c74; transient=98f0da06-ba8e-4254-8812-efc19931edaa; transient2=c19482f7-1eec-4b97-8cb2-2f66937854c4;
第二次请求:
singleton=ebece97f-bd38-431c-9fa0-d8af0419dcff; scoped1=f5397c05-a418-4f92-8c6d-78c2c8359bb5; scoped2=f5397c05-a418-4f92-8c6d-78c2c8359bb5; transient=59ed30fa-609b-46b1-8499-93a95ecd330b; transient2=d2a8ea1c-ae0b-4732-b0a1-ca186897e676;
用guid来表示不同的实例。对比两次请求可见addsingleton方式的id值相同;addscope方式两次请求之间不同,但同一请求内是相同的;addtransient方式在同一请求内的多次注入间都不相同。
另外还有tryadd{lifetime}方式,如果只希望在同类型的服务尚未注册时才添加服务,可以使用这种方法。如果直接使用add{liffetime},则多次使用会重复注册。
除了add{lifetime}<{service}, {implementation}>()这种用法,还可以这另一个重载写法:
add{lifetime}(typeof(service, typeof(implementation)
这种写法还有个好处是可以解析泛型,像ilogger就是框架利用这种方式自动注册的:
services.addsingleton(typeof(ilogger<>), typeof(logger<>));
以链式方式使用依赖关系注入时,每个请求的依赖关系相应地请求其自己的依赖关系。容器会解析这些依赖关系,构建“依赖关系树”,并返回完全解析的服务。在链式注入时需要注意依赖方的生命周期不能大于被依赖方的生命周期。
前面例子中的scopeandtransienttester把三种生命周期的服务都注入了,那么它就只能注册为暂时的,否则启动时会报错:
system.aggregateexception: 'some services are not able to be constructed (error while validating the service descriptor '*** lifetime: singleton implementationtype: ***': cannot consume scoped service '***' from singleton
内置的服务容器旨在满足框架和大多数开发者应用的需求,一般使用内置容器就已足够,除非需要使用某些不受内置容器支持的特定功能如属性注入、基于名称的注入、子容器、自定义生存期管理、对迟缓初始化的 func
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论