Example #1
0
        public void SnapshotOptionsAreCachedPerScope()
        {
            var config   = new ConfigurationBuilder().AddInMemoryCollection().Build();
            var services = new ServiceCollection()
                           .AddOptions()
                           .AddSingleton <IConfigureOptions <FakeOptions>, TestConfigure>()
                           .AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(new ConfigurationChangeTokenSource <FakeOptions>(Options.DefaultName, config))
                           .AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(new ConfigurationChangeTokenSource <FakeOptions>("1", config))
                           .BuildServiceProvider();

            var         factory  = services.GetRequiredService <IServiceScopeFactory>();
            FakeOptions options  = null;
            FakeOptions namedOne = null;

            using (var scope = factory.CreateScope())
            {
                options = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Value;
                Assert.Equal(options, scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Value);
                Assert.Equal(1, TestConfigure.ConfigureCount);
                namedOne = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Get("1");
                Assert.Equal(namedOne, scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Get("1"));
                Assert.Equal(2, TestConfigure.ConfigureCount);

                // Reload triggers Configure for the two registered change tokens.
                config.Reload();
                Assert.Equal(4, TestConfigure.ConfigureCount);

                // Reload should not affect current scope.
                var options2 = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Value;
                Assert.Equal(options, options2);
                var namedOne2 = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Get("1");
                Assert.Equal(namedOne, namedOne2);
            }
            Assert.Equal(1, TestConfigure.CtorCount);

            // Reload should be reflected in a fresh scope.
            using (var scope = factory.CreateScope())
            {
                var options2 = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Value;
                Assert.NotEqual(options, options2);
                Assert.Equal(4, TestConfigure.ConfigureCount);
                var namedOne2 = scope.ServiceProvider.GetRequiredService <IOptionsSnapshot <FakeOptions> >().Get("1");
                Assert.NotEqual(namedOne, namedOne2);
                Assert.Equal(4, TestConfigure.ConfigureCount);
            }
            Assert.Equal(1, TestConfigure.CtorCount);
        }
Example #2
0
 public ControllerWithMonitor(IOptionsMonitor <FakeOptions> watcher)
 {
     _watcher = watcher.OnChange(o => _options = o);
 }
Example #3
0
 public ControllerWithSnapshot(IOptionsSnapshot <FakeOptions> snap)
 {
     _options = snap.Value;
 }