131

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

public class CustomSection 
{
   public int A {get;set;}
   public int B {get;set;}
}

//In Startup.cs
services.Configure<CustomSection>(Configuration.GetSection("CustomSection"));

//Inject an IOptions instance
public HomeController(IOptions<CustomSection> options) 
{
    var settings = options.Value;
}

I have an appsettings.json section who's key/value pairs will vary in number and name over time. Therefore, it's not practical to hard code property names in a class since new key/value pairs would require a code change in the class. A small sample of some key/value pairs:

"MobileConfigInfo": {
    "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."
}

Is there a way to load this data into a MobileConfigInfo Dictionary<string, string> object and then use the IOptions pattern to inject MobileConfigInfo into a controller?

2
  • 1
    Hmmm isnt this question really about ASP.NET Core and not .NET Core? Bit of a misleading title.
    – bytedev
    Commented Feb 7, 2018 at 12:21
  • Interestingly this doesn't seem to work for me with certain keys. For example I have { "foo": "bar", "openid email api://50095d53-b75e-4c70-951d-8e309168a22f/example": "example key" } and interestingly the "foo": "bar" key does come through but the second key does not. Is there a way to have any arbitrary key come through? Commented Jan 12 at 17:26

14 Answers 14

98

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);
});
4
  • 2
    This worked for me. Also works if the dictionary is deeper down the section configuration, with the bind call as is. Commented Apr 18, 2018 at 16:59
  • 10
    Adding to this, so long as you use services.AddOptions() from Microsoft.Extensions.Options, you don't need to use Bind. This is enough: services.Configure<CustomSection>(Configuration.GetSection("MobileConfigInfo")); Commented Jan 25, 2021 at 18:42
  • I can confirm that services.Configure<CustomSection>(Configuration.GetSection("MobileConfigInfo")); Works well in .NET 7 without the Bind. And probably works prior to .NET 7.
    – irperez
    Commented Aug 17, 2023 at 13:51
  • 1
    I'm happy to report that this works with more complex dictionary values as well, e.g., Dictionary<string, MyRecord>
    – Doug
    Commented Dec 6, 2023 at 12:34
97

For others who want to convert it to a Dictionary,

sample section inside appsettings.json

"MailSettings": {
    "Server": "http://mail.mydomain.com"        
    "Port": "25",
    "From": "[email protected]"
 }

Following code should be put inside the Startup file > ConfigureServices method:

public static Dictionary<string, object> MailSettings { get; private set; }

public void ConfigureServices(IServiceCollection services)
{
    //ConfigureServices code......

    MailSettings = Configuration.GetSection("MailSettings").GetChildren()
                  .ToDictionary(x => x.Key, x => x.Value);
}

Now you can access the dictionary from anywhere like:

string mailServer = Startup.MailSettings["Server"];

One downside is that all values will be retrieved as strings, if you try any other type the value will be null.

3
  • 9
    You don't need the .Select(item => new KeyValuePair<string, string>(item.Key, item.Value)) part. You can just call GetChildren().ToDictionary(x => x.Key, x => x.Value).
    – Alternatex
    Commented Nov 5, 2018 at 9:14
  • 1
    this should be the accepted answer, this is by far the simplest and most elegant solution. Commented Sep 10, 2020 at 8:21
  • 1
    If you need a non-interfaced Dictionary<string,string> then you can call new Dictionary<string, string>(Configuration.GetSection("mysettings").AsEnumerable())
    – Dan
    Commented Nov 24, 2020 at 17:00
52

I believe you can use the following code:

var config =  Configuration.GetSection("MobileConfigInfo").Get<Dictionary<string, string>>(); 
0
39

You can use Configuration.Bind(settings); in startup.cs class

And your settings class will be like

public class AppSettings
{
    public Dictionary<string, string> MobileConfigInfo
    {
        get;
        set;
    }
}

Hope it helps!

3
  • I am trying to understand the example you provided, but it is very confusing. What is settings?
    – Catalin
    Commented Jul 18, 2018 at 11:06
  • 1
    @Catalin settings is an instance of AppSettings class. The Bind method will map the values in your config.json to your settings instance. After that you can use .AddSingleton or something like it to inject it to other classes
    – Hung Cao
    Commented Jul 18, 2018 at 19:36
  • 9
    This does not work for classes with a Dictionary<string, string> property. The Bind method fails at runtime with: System.InvalidOperationException: 'Cannot create instance of type 'System.String' because it is missing a public parameterless constructor.' Commented Jan 15, 2020 at 15:31
37

By far the simplest method would be to define your configuration class to inherit from the Dictionary type you want to support.

public class MobileConfigInfo:Dictionary<string, string>{
}

Then your startup and dependency injection support would be exactly the same as for any other configuration type.

5
  • 1
    By far the simplest. Now if there were just an easy way to use an integer as a key...
    – Airn5475
    Commented Aug 29, 2019 at 19:57
  • 1
    That's really a JSON limitation since it only allows for string keys. Commented Aug 30, 2019 at 1:29
  • I think this should be the accepted answer. Something helpful to incorporate MIGHT be eager options validation. In my case, I wanted a dictionary of enums to enums, but didn't want to force every enum value to be defined in the config. Here is a way to do eager validation: github.com/aspnet/Extensions/issues/459#issuecomment-536547483
    – bmw15
    Commented Dec 9, 2019 at 16:46
  • You are a god among men Commented Jan 14, 2021 at 13:19
  • this syntax is brilliant. but how to make it work?
    – Hoy Cheung
    Commented Feb 19 at 17:47
15

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.

10

I use the way below:

appsettings.json:

  "services": {
      "user-service": "http://user-service:5000/",
      "app-service": "http://app-service:5000/"
  } 

startup.cs:

  services.Configure<Dictionary<string, string>>(Configuration.GetSection("services"));

Usage:

private readonly Dictionary<string, string> _services;
public YourConstructor(IOptions<Dictionary<string, string>> servicesAccessor)
{
    _services = servicesAccessor.Value;
}
1
  • 1
    Not sure why this isn't the top answer as nearly every other answer missed the fact the OP wanted to consume them through IOptions and dependency injection. This has really helped me anyway, thank you.
    – Matt
    Commented Feb 3, 2021 at 17:16
9

You can do it on the fly:

appsettings.json:
{
   "MobileConfigInfo": {
      "a": "x",
      "b": "y",
      "c": "z"
    }
}

Somewhere in code: (don't forget to add IConfiguration dependency to the class constructor)

var yourDictionary = _configuration.GetSection("MobileConfigInfo")
                .Get<IDictionary<string, string>>();
8

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
}
5

The only thing that worked for me (ASP.NET Core 3.0) was to add the following to the ConfigureServices method of Startup.cs:

services.Configure<Dictionary<string, string>>(dict => Configuration
    .GetSection("MySectionName")
    .GetChildren()
    .ToList()
    .ForEach(c => dict[c.Key] = c.Value));
4

As an example of more complex binding in ASP.Net Core 2.1; I found using the ConfigurationBuilder .Get<T>() method far easier to work with, as per the documention.

ASP.NET Core 1.1 and higher can use Get, which works with entire sections. Get can be more convenient than using Bind.

I bound the configuration in my Startup method.

private Config Config { get; }

public Startup(IConfiguration Configuration)
{
    Config = Configuration.Get<Config>();
}

This binds the appsettings file:

{
    "ConnectionStrings": {
        "Accounts": "Server=localhost;Database=Accounts;Trusted_Connection=True;",
        "test": "Server=localhost;Database=test;Trusted_Connection=True;",
        "Client": "Server=localhost;Database={DYNAMICALLY_BOUND_CONTEXT};Trusted_Connection=True;",
        "Support": "Server=localhost;Database=Support;Trusted_Connection=True;"
    },
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        }
    },
    "Plugins": {
        "SMS": {
            "RouteMobile": {
                "Scheme": "https",
                "Host": "remote.host",
                "Port": 84567,
                "Path": "/bulksms",
                "Username": "username",
                "Password": "password",
                "Source": "CompanyName",
                "DeliveryReporting": true,
                "MessageType": "Unicode"
            }
        },
        "SMTP": {
            "GenericSmtp": {
                "Scheme": "https",
                "Host": "mail.host",
                "Port": 25,
                "EnableSsl": true,
                "Username": "[email protected]",
                "Password": "password",
                "DefaultSender": "[email protected]"
            }
        }
    }
}

Into this configuration structure:

[DataContract]
public class Config
{
    [DataMember]
    public Dictionary<string, string> ConnectionStrings { get; set; }
    [DataMember]
    public PluginCollection Plugins { get; set; }
}

[DataContract]
public class PluginCollection
{
    [DataMember]
    public Dictionary<string, SmsConfiguration> Sms { get; set; }
    [DataMember]
    public Dictionary<string, EmailConfiguration> Smtp { get; set; }
}

[DataContract]
public class SmsConfiguration
{
    [DataMember]
    public string Scheme { get; set; }
    [DataMember]
    public string Host { get; set; }
    [DataMember]
    public int Port { get; set; }
    [DataMember]
    public string Path { get; set; }
    [DataMember]
    public string Username { get; set; }
    [DataMember]
    public string Password { get; set; }
    [DataMember]
    public string Source { get; set; }
    [DataMember]
    public bool DeliveryReporting { get; set; }
    [DataMember]
    public string Encoding { get; set; }
}

[DataContract]
public class EmailConfiguration
{
    [DataMember]
    public string Scheme { get; set; }
    [DataMember]
    public string Host { get; set; }
    [DataMember]
    public int Port { get; set; }
    [DataMember]
    public string Path { get; set; }
    [DataMember]
    public string Username { get; set; }
    [DataMember]
    public string Password { get; set; }
    [DataMember]
    public string DefaultSender { get; set; }
    [DataMember]
    public bool EnableSsl { get; set; }
}
1
  • This is in fact the best solution. Note, it's possible to specify a section, e.g. GetSection("Plugins") but then this section itself cannot be part of the binding. Although even without such specifying Get<T> will bind only existed models and ignore all other parts of json. Perfect! Also, since Net Core 3.5 class attributes are optional.
    – Niksr
    Commented May 16, 2021 at 13:20
2
You can do this in .Net Core 6. Please find the following code sample
**appsettings.json**
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AppSettings": {
    "dbConnection": "Data Source=myServerName;Initial Catalog=dbName;persist security info=True;User Id=userId;Password=testPWD;MultipleActiveResultSets=True",
    "sendereMail": "[email protected]",
    "MQDetails": {
      "hostName": "testHost",
      "username": "testUser",
      "passWord": "testPwd",
      "exchangeName": "testName"
    }
  }
}
**AppSettings.cs**
public class AppSettings
    {
        public string? dbConnection { get; set; }
        public string? sendereMail { get; set; }
        public Dictionary<string, string>? MQDetails { get; set; }
    }
**Program.cs**

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers();
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.AddConnections();
  
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    //The generated Swagger JSON file will have these properties.
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "My API POC",
        Version = "v1",
    });
});


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyPOC");
        c.RoutePrefix = string.Empty;
    });
    app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
});

app.Run();

**ApiController**
namespace DotNet6.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class GetConfigurationsController : ControllerBase
    {
        private readonly AppSettings appSettings;
        public GetConfigurationsController(IOptions<AppSettings> options)
        {
            appSettings = options.Value;
        }

        [HttpGet("appsettings")]
        public AppSettings Get()
        {

            return appSettings;
        }
    }
}

enter image description here enter image description here

1

The better way for me without changing appsetting:

"CustomSection": {
    "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."
}


public class CustomSection : Dictionary<string, string> {}

//In Startup.cs
services.AddOptions<CustomSection>().Bind(Configuration.GetSection("CustomSection"));

//Inject an IOptions instance
public HomeController(IOptions<CustomSection> options) 
{
    var settings = options.Value;
}
0

I have a generic solution for setting dictionary type properties, such as the dictionary of HTML attributes that are retrieved from options.

Default dictionary values ​​can be set in options. If the same key exists in the section, then the value in the dictionary is overwritten, otherwise a key-value pair is inserted.

The dictionary is of type IDictionary<string, object> and the read values ​​are not parsed, they are set as type string.

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class JJurOptionsExtensions
    {
        /// <summary>
        /// Binding configuration of the property of type IDictionary {string, object}
        /// </summary>
        /// <typeparam name="TOptions">
        /// The type of class that contains the property to be set
        /// </typeparam>
        /// <param name="services">
        /// IoC container
        /// </param>
        /// <param name="section">
        /// Section containing key-value pairs for the dictionary to be set
        /// </param>
        /// <returns>
        /// IServiceCollection
        /// </returns>
        /// <param name="property">
        /// Delegate of the property to be set
        /// </param>
        public static IServiceCollection ConfigureDictionary<TOptions>(
            this IServiceCollection services,
            IConfigurationSection section,
            Func<TOptions, IDictionary<string, object>> property)
                where TOptions : class
        {
            var values = section        // List of sub-sections
                .GetChildren()
                .ToList();

            services.Configure<TOptions>(options =>
            {
                var dict = property(options);
                values.ForEach(v =>
                {
                    // If there is not key, then insert it.
                    // If there is, override the value.

                    dict[v.Key] = v.Value;
                });
            });

            return services;
        }
    }
}

An example of use:

        services.Configure<JJurCoreLibs.HtmlSortMnu.SortMenuOptions>(
                options => configuration.GetSection("SortMenuOptions").Bind(options)
            )
            .ConfigureDictionary<JJurCoreLibs.HtmlSortMnu.SortMenuOptions>(
                configuration.GetSection("SortMenuOptions:DropDownBbtnHtmlAttributes"),
                o => o.DropDownBbtnHtmlAttributes);

The SortMenuOptions class contains a property named DropDownBtnHtmlAttribute of type Dictionary<string, object>.

using System.Collections.Generic;

namespace JJurCoreLibs.HtmlSortMnu
{
    /// <summary>
    /// Options of the Bootstrap dropdown creating service for sorting items
    /// </summary>
    public class SortMenuOptions
    {
...
        public DropDownBbtnHtmlAttributes DropDownBbtnHtmlAttributes { get; } = new DropDownBbtnHtmlAttributes {
            { "role", "button" },
            { "data-toggle", "dropdown" },
            { "aria-expanded", false },
            { "aria-haspopup", true }
        };
    }

    public class DropDownBbtnHtmlAttributes : Dictionary<string, object> { }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.