IConfigureOptions<T> 未创建作用域选项



通常是 Options。但是,我正在从数据库中构建选项,并且其中一个选项属性是密码,它每月都在不断更改。因此,我想创建选项的Scoped实例。我正在使用以下IConfigureOptions<T>来构建数据库

的选项
public class MyOptions
{
   public string UserID {get;set;}
   public string Password {get;set;
}
public class ConfigureMyOptions : IConfigureOptions<MyOptions>
{
    private readonly IServiceScopeFactory _serviceScopeFactory;
    public ConfigureMyOptions(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceScopeFactory = serviceScopeFactory;
    }
    public void Configure(MyOptions options)
    {
        using (var scope = _serviceScopeFactory.CreateScope())
        {
            var provider = scope.ServiceProvider;
            using (var dbContext = provider.GetRequiredService<MyDBContext>())
            {
                options.Configuration = dbContext.MyOptions
                                        .SingleOrDefault()
                                        .Select(x => new MyOptions()
                                        {
                                            UserID = x.UserID,
                                            Password = x.Password
                                        });
            }
        }
    }
}

在控制器中使用它

    public class HomeController : BaseController
    {
        private readonly MyOptions _options;
        public HomeController(IOptions<MyOptions> option)
        {
            _options = option.Value;
        }
        [HttpGet]
        [Route("home/getvalue")]
        public string GetValue()
        {
            // do something with _options here
            return "Success";
        }
    }

我想为每个新请求创建MyOptions的实例,因此请在startup.cs

中注册为Scoped
services.AddScoped<IConfigureOptions<MyOptions>, ConfigureMyOptions>();

但是,当我将调试器放入ConfigureMyOptions的配置方法中时,仅对第一个请求命中一次。对于下一个请求,容器返回同一实例(例如Singleton(。

如何在此处设置范围,以便为每个请求创建肌肉?

在您的控制器中使用IOptionsSnapshot代替IOptions,它将每个请求重新创建选项。


为什么不使用IOptions

.addoptions扩展方法API的配置方法将OptionsManager实例作为IOptions<>

singlethon
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));

OptionsManager类在内部使用缓存:

 public virtual TOptions Get(string name)
 {
     name = name ?? Options.DefaultName;
     // Store the options in our instance cache
     return _cache.GetOrAdd(name, () => _factory.Create(name));
 }

github上的以下问题帮助以上找到:optionssnapshot应始终根据请求重新创建

最新更新