Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.
This repository was archived by the owner on Nov 7, 2018. It is now read-only.

IOptionsChangeTokenSource does not work with scoped IConfigureOptions #199

Closed
@pakrym

Description

@pakrym

Not sure if this is intended behavior but it is confusing.
Consider the following example:

 public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IConfigureOptions<ScopedOptions>, ScopedConfigureOptions>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                var optionsSnapshot = context.RequestServices.GetService<IOptionsSnapshot<ScopedOptions>>();
                await context.Response.WriteAsync(optionsSnapshot.Value.I.ToString());
            });
        }
    }

    public class ScopedConfigureOptions : IConfigureOptions<ScopedOptions>
    {
        private int _i;
        private static int _global;

        public ScopedConfigureOptions()
        {
            _i = _global++;
        }

        public void Configure(ScopedOptions options)
        {
            options.I = _i;
        }
    }

    public class ScopedOptions
    {
        public int I { get; set; }
    }

I would expect OptionsSnapshot being scoped to initialize options instance using new instance of scoped IConfigureOptions every time instead last first one is cached forever (if IOptionsChangeTokenSource is not used)

@HaoK

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions