1

I am migrating an Asp.Net MVC application to .Net5. I had a static class that served as a façade for the settings in web.config. My Setting class exposed static properties, each one of a class representing settings groups, for example:

public static class MySettings
{
    public static class MainDB
    {
        public static string ConnectionString
        {
            get
            {
                // Code to retrieve the actual values
                return "";
            }
        }
    }

    public static class ExternalApi
    {
        public static string Uri
        {
            get
            { // Code to retrieve the actual values
                return "";
            }
        }
    }

    public static class OtherSettings
    {
        // ...
    }
}

In .Net Core 5 (actually, since .Net Core 2) we use POCO's tyo read settings as depicted in https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0

Is there any way to map all my settings to one single object, for example, for appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Settings": {
    "MainDB": {
      "ConnectionString": "whatever needed to access the db"
    },
    "ExtrenaAPI" {
      "Uri": "https://whatever.api.com",
      "Key": "mysupersecretkey",
      "Secret": "mysupersecret-uh-secret"
    }
  }
}

Classes:

public class MainDB
{
    public string ConnectionString { get; set; }
}

public class ExternalApi
{
    public string Uri { get; set; }
    public string Key { get; set; }
    public string Secret { get; set; }
}

public class Settings
{
    public MainDB MainDB { get; set; }

    `public ExternalApi ExternalApi { get; set; }
}

Configuration (in Startup.cs):

services.Configure<Settings>(Configuration.GetSection("Settings"));

(Yes, I know I can do services.Configure<MainDB>(Configuration.GetSection("Settings:MainDB")); and services.Configure<ExternalApi>(Configuration.GetSection("Settings:ExternalApi")); but I'd like to get all the settings in one single object if it is possible.

Any suggestion?

2 Answers 2

2

Am assuming here you're talking about binding (app settings file to a single object?) If you're okay with bit of extra code then this could work for what you want to achieve.

IConfigurationRoot configRoot = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Settings settingsRoot = new Settings(); // custom POCO with appsettings structure
((IConfiguration)configRoot).Bind(settingsRoot);

public class Settings
{
    public Logging Logging { get; set; }

    public string AllowedHosts { get; set; }
}
public class LogLevel
{
    public string Default { get; set; }
    public string Microsoft { get; set; }
}

If you're only trying to setup a single node/section's hierarchy you can simply do a ((IConfiguration)config.GetSection("SectionName")).Bind(myObject)

Either way config.Bind(object) is the magic bit here.

Sign up to request clarification or add additional context in comments.

2 Comments

I did exactly same code in my console app and I don't have any Bind() method. Where is this coming from?
@BastienVandamme Install the Microsoft.Extensions.Configuration nuget package
0

IConfiguration Configuration is your facade to the appsettings (actually, to all settings, whether they are coming from appsettings, user secrets, or wherever). You can use

var configurationSection = Configuration.GetSection("Settings") 

to get to specific section, and then maybe have this as private static variable in MySettings class - but it seems redundant. And having internal class for something that can be easily retrieved as Configuration[key] seems double-redundant

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.