How to load appsetting.json section into Dictionary in .NET Core?

后端 未结 10 1480
误落风尘
误落风尘 2020-12-05 16:49

I am familiar w/ loading an appsettings.json section into a strongly typed object in .NET Core startup.cs. For example:

public class CustomSection 
{
   publ         


        
相关标签:
10条回答
  • 2020-12-05 17:38

    Go with this structure format:

    "MobileConfigInfo": {
        "Values": {
           "appointment-confirmed": "We've booked your appointment. See you soon!",
           "appointments-book": "New Appointment",
           "appointments-null": "We could not locate any upcoming appointments for you.",
           "availability-null": "Sorry, there are no available times on this date. Please try another."
     }
    }
    

    Make your setting class look like this:

    public class CustomSection 
    {
       public Dictionary<string, string> Values {get;set;}
    }
    

    then do this

    services.Configure<CustomSection>((settings) =>
    {
         Configuration.GetSection("MobileConfigInfo").Bind(settings);
    });
    
    0 讨论(0)
  • 2020-12-05 17:40

    In .NET Core 3.1 you can do something like the following...

    appsettings.json:

    {
      "myConfig": {
        "foo": "bar",
        "myMappings": {
          "key1": "value1",
          "key2": "value2"
        }
      }
    }
    

    A configuration model

    MyConfig.cs

    public class MyConfig
    {
        public string Foo { get; set; }
        public Dictionary<string, string> MyMappings { get; set; }
    }
    

    Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MyConfig>(configuration.GetSection("myConfig"));
    

    Class using the options:

    public class OptionsUsingClass
    {
        public OptionsUsingClass(IOptions<MyConfig> myConfigOptions)
        {
            // Be wary of nulls in real code.
            var myConfig = myConfigOptions.Value;
    
            // Examples with the above data.
            myConfig.Foo.Should().Be("bar");
    
            myConfig.MyMappings["key1"].Should().Be("value1");
            myConfig.MyMappings["key2"].Should().Be("value2");
        }
    

    This was how I used appsettings.json dictionary mappings.

    0 讨论(0)
  • 2020-12-05 17:47

    I believe you can use the following code:

    var config =  Configuration.GetSection("MobileConfigInfo").Get<Dictionary<string, string>>(); 
    
    0 讨论(0)
  • 2020-12-05 17:48

    For simple (perhaps microservice) applications you can just add it it as a singleton Dictionary<string, string> and then inject it wherever you need it:

    var mobileConfig = Configuration.GetSection("MobileConfigInfo")
                        .GetChildren().ToDictionary(x => x.Key, x => x.Value);
    
    services.AddSingleton(mobileConfig);
    

    And the usage:

    public class MyDependantClass
    {
        private readonly Dictionary<string, string> _mobileConfig;
    
        public MyDependantClass(Dictionary<string, string> mobileConfig)
        {
            _mobileConfig = mobileConfig;
        }
    
        // Use your mobile config here
    }
    
    0 讨论(0)
提交回复
热议问题