Roy*_*mir 6 c# dependency-injection .net-core asp.net-core
我有 3 个接口(用于单例/作用域/瞬态):
public interface ISingleton
{
}
class Singlet : ISingleton
{
}
public interface IScoped
{
}
class Scoped : IScoped
{
}
public interface Itransient
{
}
class Transient : Itransient
{
}
Run Code Online (Sandbox Code Playgroud)
我将它们注册为:
services.AddScoped<IScoped, Scoped>();
services.AddTransient<Itransient, Transient>();
services.AddSingleton<ISingleton, Singlet>();
Run Code Online (Sandbox Code Playgroud)
如果我尝试将作用域服务注入 singleton ,我应该(并且确实)得到一个异常(我知道其原因):
public interface ISingleton
{
}
class Singlet : ISingleton
{
public Singlet( IScoped sc)
{
}
}
Run Code Online (Sandbox Code Playgroud)
某些服务无法构建(验证服务描述符“ServiceType:WebApplication2.ISingleton Lifetime:Singleton ImplementType:WebApplication2.Singlet”时出错:无法使用单例“WebApplication2.ISingleton”中的作用域服务“WebApplication2.IScoped”。)
但如果我尝试注入瞬态,我不会得到异常:
public interface ISingleton
{
}
class Singlet : ISingleton
{
public Singlet( Itransient tr)
{
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
为什么 .net core 禁止注入较短生命周期(作用域)的服务引发异常,同时允许将瞬态注入到单例中?
Dai*_*Dai 10
我建议先阅读以下内容:When are .NET Core dependency injectionInstances Dispose?
(前言:我的回答假设服务实现永远不会自行处置任何注入的服务。)
为什么 .net core 禁止注入较短生命周期(作用域)的服务引发异常,同时允许将瞬态注入到单例中?
IHost.Dispose())简而言之:瞬态服务的生命周期与其注入的服务相同——不长,也不短。这就是为什么它是被允许的。
鉴于作用域服务的生命周期比根作用域/根容器的生命周期短,并且根作用域/根容器用于保存单例实例,因此作用域服务不能被单例使用(更重要的是它是因为子作用域不存在于根作用域的上下文中)。
| 归档时间: |
|
| 查看次数: |
1799 次 |
| 最近记录: |