IConfigureOptions<T> is not creating scoped options
Asked Answered
S

1

8

Typically Options are singleton. However i am building options from the database, and one of the Options property is password which keep changing every month. So i wanted to create Scoped instance of Options. I am using IConfigureOptions<T> like below to build Options from the database

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
                                        });
            }
        }
    }
}

Use it in controller

    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";
        }
    }

I want to create an instance of MyOptions for every new request so register it as Scoped in startup.cs

services.AddScoped<IConfigureOptions<MyOptions>, ConfigureMyOptions>();

However, when i put debugger inside ConfigureMyOptions's Configure method it only gets hit once for the first request. For next request onward the container returns the same instance (like singleton).

How do i set the scope here so MyOptions will get created for each request?

Sacerdotalism answered 21/2, 2018 at 20:4 Comment(1)
Is it possible to use this code to modify IConfigureOptions from controller? Because I can't update the values. ThxIllogic
S
8

Use IOptionsSnapshot instead of IOptions in your controller and it will recreate options per request.


Why doesn't work with IOptions:

.AddOptions extension method of Configuration API registers the OptionsManager instance as a singlethon for IOptions<>

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

and OptionsManager class uses caching internally:

 public virtual TOptions Get(string name)
 {
     name = name ?? Options.DefaultName;

     // Store the options in our instance cache
     return _cache.GetOrAdd(name, () => _factory.Create(name));
 }

The following issue on github helped to find above: OptionsSnapshot should always be recreated per request

Sabelle answered 21/2, 2018 at 21:43 Comment(2)
Why this happens: Captive DependencyLiver
that worked!! I did not know there was IOptionsSnapshot option tooSacerdotalism

© 2022 - 2024 — McMap. All rights reserved.