use*_*079 2 c# dependency-injection .net-core asp.net-core
当配置对象通过以下方式注册时,它的生命周期是哪个?它是单例、瞬态还是作用域?
{
services.Configure<SomeConfiguration>(configuration.GetSection(nameof(SomeConfiguration)));
}
Run Code Online (Sandbox Code Playgroud)
这就是我将配置添加到服务类中的方法
public class SomeService: ISomeService
{
public SomeService(IOptionsMonitor<SomeConfiguration> configuration)
{
_configuration = configuration.CurrentValue;
}
}
Run Code Online (Sandbox Code Playgroud)
通过调试很难知道它是否是单例。因为 IOptionsMonitor 能够通过在 appsettings.json 更改上运行观察程序来检测运行时的更改