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
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);
});
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.
I believe you can use the following code:
var config = Configuration.GetSection("MobileConfigInfo").Get<Dictionary<string, string>>();
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
}