简体   繁体   English

具有默认动态值的 IOptions 模式未按预期工作

[英]IOptions pattern with default dynamic value not working as expected

I have a class which contains my configuration of a StartDate string, it is a yyyyMMdd hh:mm:ss representation of a timestamp, this string can be passed through appsettings or environment variables as normal with ASP.NET applications, but when it is not set, I would like it to return the current time.我有一个 class ,其中包含我对StartDate字符串的配置,它是时间戳的yyyyMMdd hh:mm:ss表示形式,此字符串可以像 ASP.NET 应用程序一样通过 appsettings 或环境变量传递,但如果未设置,我希望它返回当前时间。

public class MyOptions
{
    public string StartDate { get; set; } = DateTime.Now.ToString("yyyyMMdd hh:mm:ss");
}

In my Startup I register this configuration class like so:在我的启动中,我像这样注册此配置 class :

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));
        ...
    }
    ...
}

I then inject it in another service and use it like so:然后我将它注入另一个服务并像这样使用它:

public class SomeOtherService
{
    private readonly MyOptions _options;
    public SomeOtherService(IOptions<MyOptions> options)
    {
        _options = options.Value;
    }
    
    void SomeFunction()
    {
        Console.WriteLine($"StartDate is: {_options.StartDate}");
    }
}

My AppSettings(.Development).json nor environment variables contain a value for MyOptions.StartDate我的 AppSettings(.Development).json 环境变量也不包含MyOptions.StartDate的值

Whenever I call SomeOtherClass.SomeFunction() , it will keep returning whatever the timestamp was when my app first started.每当我调用SomeOtherClass.SomeFunction()时,它都会继续返回我的应用程序首次启动时的时间戳。

I have split the StartDate property in MyOptions into a separate getter and setter class, and noticed the setter is being hit while the application starts.我已将MyOptions中的StartDate属性拆分为单独的 getter 和 setter class,并注意到在应用程序启动时 setter 被击中。 It seems it's reading the initial return value from DateTime.Now.ToString("yyyyMMdd hh:mm:ss") , but then setting this value, causing the set value to returned in any consecutive get call.似乎它正在从DateTime.Now.ToString("yyyyMMdd hh:mm:ss")读取初始返回值,但随后设置此值,导致在任何连续的 get 调用中返回设置值。

What am I missing to get it to return the current timestamp whenever it is called?我错过了什么让它在被调用时返回当前时间戳?

I was able to get it working using the code like below我能够使用下面的代码让它工作

services.Configure<MyOptions>(_ => Configuration.GetSection("MyOptions").Bind(_));

the appsettings.json should have MyOptions element like below appsettings.json应该有如下MyOptions元素

"MyOptions": {"StartDate": "test"}

Please check that environment name matches with environment specific appsettings if you are modifying not the global appsettings.json file.如果您修改的不是全局appsettings.json文件,请检查环境名称是否与特定于环境的 appsettings 匹配。 It means that environment name have to be Development if appsettings.Development.json has MyOptions configuration element.这意味着如果appsettings.Development.json具有MyOptions配置元素,则环境名称必须是Development

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM