1

I am trying to get settings from my config server and map it to my object. However IConfiguration is returning me a collection of Providers and then I have to use the GetSection or GetChildern method to get configuration settings.

e.g.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyConfigurations>(Configuration.GetSection("spring:cloud:config"));
}

The above gets me a particular section and am able to map it to my MyConfiguration class properties.

However there are more sections I need to target. I don't want to do .GetSection to get them one by one.

Is there anything I can use to get a collection from the required provider i.e. SteelToe so that I can map it to the properties defined inside my config class?

KJSR
  • 1,679
  • 6
  • 28
  • 51

1 Answers1

1

You can create a mapping class for your configuration, like this:

public class ConfigSettings
{
    public string ConfigSetting1 { get; set; }
    public string ConfigSetting2 { get; set; }
    public string ConfigSetting3 { get; set; }
    public SubConfigSettings1 SubConfigSettings1 { get; set; }
}

public class SubConfigSettings1 
{
    public string SubConfigSetting1 { get; set; }
    public string SubConfigSetting2 { get; set; }
}

and fetch them using,

var setting = Configuration.Get<ConfigSettings>();

EDIT:

if you have this steeltoe config

{
  "spring": {
    "cloud": {
      "config": {
        "uri": "http://localhost:8888"
      }
    }
  },
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Console": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  }
}

You can define the ConfigSettings class like this.

public class ConfigSettings
{
    public Spring spring { get; set; }
    public Logging Logging { get; set; }
}

public class Spring
{
    public Cloud cloud { get; set; }
}

public class Cloud
{
    public Config config { get; set; }
}

public class Config
{
    public string uri { get; set; }
}

public class Logging
{
    public bool IncludeScopes { get; set; }
    public Loglevel LogLevel { get; set; }
    public Console Console { get; set; }
}

public class Console
{
    public Loglevel LogLevel { get; set; }
}

public class Loglevel
{
    public string Default { get; set; }
    public string System { get; set; }
    public string Microsoft { get; set; }
}

and use like this.

services.Configure<ConfigSettings>(Configuration);

and use the following to access uri section, for example.

var settings = Configuration.Get<ConfigSettings>();
string springCloudConfigUri = settings.spring.cloud.config.uri;

here, the Configuration is IConfiguration

Nouman
  • 591
  • 6
  • 14
  • Thank you Nouman for your answer. So if my provider is `Steeltoe.Extensions.Configuration.ConfigServer.ConfigServerConfigurationProvider` then will I name my Property inside the `ConfigSettings` as `public string ConfigServerConfigurationProvider`? – KJSR Jul 12 '19 at 12:23
  • 1
    Yes, the names should match for binding to work. [ConfigurationBinder.Get Method](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder.get?view=aspnetcore-2.2) – Nouman Jul 12 '19 at 12:27
  • Hi Nouman apologies if I am getting this wrong? So in my startup.cs I have this `services.Configure(Configuration);` and then inside `MyConfigurations` class I have a property called `public string ConfigServerConfigurationProvider` shouldn't this be mapping it automatically? Property is null – KJSR Jul 12 '19 at 12:47
  • please take a look at the edit, does it answer your question? – Nouman Jul 12 '19 at 13:02
  • Thank you again Nouman for this. Makes a lot more sense now :) I will mark this as answer. Just to ask one more thing, to map a setting like `ff-test-data` will i need to use something like `[DataMember(Name="ff-test-data")] public string testData {get;set;}`? – KJSR Jul 12 '19 at 14:01
  • I'm not sure about that, you can try DataMember or JsonProperty to see if it works. Otherwise you might need to create custom binding. – Nouman Jul 12 '19 at 14:24
  • I have tried both and unfortunately none of them bind :( – KJSR Jul 12 '19 at 14:28