随笔分类 -  Net6依赖注入

摘要:.AddTransient<IFoo, Foo>() // //ImplementationType 根据类型 .AddScoped<IBar>(_ => new Bar())//ImplementationFactory 通过工厂创建 .AddSingleton<IBaz>(new Baz()); 阅读全文
posted @ 2024-03-10 16:43 孤海飞雁 阅读(26) 评论(0) 推荐(0) 编辑
摘要:依赖注入 先添加要注册的对象 如 AddTransient<IA,A>()到 ServiceCollection ;然后将添加的 对象 添加到到 容器里,这里根据 添加 对象的不同,如 .AddScoped<IB>(_ => new B()),AddSingleton<IC>(new C());等然 阅读全文
posted @ 2023-09-10 23:52 孤海飞雁 阅读(7) 评论(0) 推荐(0) 编辑
摘要:IServiceProvider 处理提供所需服务实例,有需要还会负责释放服务实例。具体操作为调试用实例的dispose或者异步dispose方法。 scoped和transient:当前Iservice provider 对象 调用dispose 方法,实例方法 dispose 也被调用,随之释放 阅读全文
posted @ 2022-10-17 22:19 孤海飞雁 阅读(310) 评论(0) 推荐(0) 编辑
摘要:public class ServicesPrpvoder:IServicesPrpvoder 服务提供者可以利用IserviceScoprFactory 创建一个 服务范围 IServiceScope对象IServicesScope 的包含IServicesPrpvoder任何一个 IServic 阅读全文
posted @ 2022-10-17 21:27 孤海飞雁 阅读(53) 评论(0) 推荐(0) 编辑
摘要:var sc = new ServiceCollection() .AddScoped<IA, A>() .AddSingleton<IB, B>() .AddTransient<IC, C>() .BuildServiceProvider(true);//ValidateScopes检查在scop 阅读全文
posted @ 2022-10-12 22:00 孤海飞雁 阅读(20) 评论(0) 推荐(0) 编辑
摘要:通过上图可知, 容器中 Transient 的foo 被每个容器创建,scoped 的 ber 也在每个容器中创建 ,二 单例的 baz 只在根容器中创建 阅读全文
posted @ 2022-10-11 21:43 孤海飞雁 阅读(16) 评论(0) 推荐(0) 编辑
摘要:使用ServiceCollection 注入AddTransient,AddScoped,AddSingleton 三不同生命周期的的对象 Transient 最先释放 Scope 随后 Singleton 最后 using (var sc = new ServiceCollection() .Ad 阅读全文
posted @ 2022-10-10 22:10 孤海飞雁 阅读(123) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示