Skip to content

IOptionsSnapshot is not working with singleton services #2380

@aspnet-hello

Description

@aspnet-hello

From @ductrantb on Thursday, December 14, 2017 12:42:32 AM

Hi friends,
I have a problem with IOptionsSnapshot. I was tried to use BackgroundServiceManagerConfigsFactory as a transient service to renew configs on change.

services.AddTransient<BackgroundServiceManagerConfigsFactory, BackgroundServiceManagerConfigsFactory>();
services.AddSingleton<BackgroundServiceManager, BackgroundServiceManager>();

The constructor of my configs factory was called on every request (by singleton service), but configs value was not changed.

public BackgroundServiceManagerConfigs Configs { get; protected set; }
public BackgroundServiceManagerConfigsFactory(IOptionsSnapshot<BackgroundServiceManagerConfigs> options)
{
        Configs = options.Value;
}

I tried to call this factory in a controller, it worked.
I used netcore 2.0 and Microsoft.Extensions.Options version 2.0.0.0
Thank you.

Copied from original issue: aspnet/Options#247

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions