App Settings .Net Core

后端 未结 2 1827
自闭症患者
自闭症患者 2021-01-17 05:15

I am trying to add an appsettings.json and followed a lot of tutorials and still can not do it.

I create appsettings.json

{
  \"option1\": \"value1_f         


        
相关标签:
2条回答
  • 2021-01-17 05:34

    Did you include the correct namespace?

    using Microsoft.Extensions.DependencyInjection;
    

    Also did you have a reference to?:

    Microsoft.Extensions.Options.ConfigurationExtensions
    

    In above Assembly we have:

    public static IServiceCollection Configure<TOptions>(this IServiceCollection services, IConfiguration config) where TOptions : class;
    

    Most probably you are using the extension method from Microsoft.Extensions.Options assembly (which is wrong)

    public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions) where TOptions : class;
    
    0 讨论(0)
  • 2021-01-17 05:39

    Make sure that you imported everything that is necessary and have the required packages installed. Then you can do the following

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

    this will cause the options to be updated at runtime whenever you change the appssettings programatically.

    0 讨论(0)
提交回复
热议问题