将lightInject转换为.netcore DI [英] Converting lightInject to .netcore DI

查看:89
本文介绍了将lightInject转换为.netcore DI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些lightInject代码转换为.netcore DI

I am converting some lightInject code to .netcore DI

在.netcore DI中,我们具有用于注册服务的AddSingleton和AddTransient函数. 在lightInject中,我有RegisterMethod.所以我只想知道Register方法在我们调用时是只注册一个实例还是多个实例.

In .netcore DI, we have AddSingleton and AddTransient functions for registering the service. In the lightInject, I have RegisterMethod. So I just want to know whether the Register methods registers only singleton instance or multiple when ever we call.

推荐答案

基于 documentation 除非另有说明,否则LightInject的默认行为是将所有对象都视为瞬态.因此,在.Net Core中,您需要将服务注册为瞬时.

based on documentation The default behavior in LightInject is to treat all objects as transients unless otherwise specified. So in .Net Core, you would need to register your services as transient.

有关寿命的一些信息:

瞬态 每次从服务容器中请求时,都会创建瞬态生存期服务(AddTransient).此生命周期最适合轻量级,无状态服务.

Transient Transient lifetime services (AddTransient) are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services.

范围 每个客户端请求(连接)都会创建一次作用域生命周期服务(AddScoped).

Scoped Scoped lifetime services (AddScoped) are created once per client request (connection).

单人 单例生存期服务(AddSingleton)是在首次请求时创建的(或者在运行Startup.ConfigureServices并使用服务注册指定实例时创建).每个后续请求都使用相同的实例.

Singleton Singleton lifetime services (AddSingleton) are created the first time they're requested (or when Startup.ConfigureServices is run and an instance is specified with the service registration). Every subsequent request uses the same instance.

这是一个链接到.Net Core依赖项注入文档.

Here is a link to .Net Core dependency injection documentation.

这篇关于将lightInject转换为.netcore DI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆