7

I'm trying to retrieve configuration inside my application.

I have passed the IConfiguration into the service class which needs to pull some settings.

The class looks a bit like this:

private IConfiguration _configuration;
public Config(IConfiguration configuration)
{
    _configuration = configuration;
}
public POCO GetSettingsConfiguration()
{
    var section = _configuration.GetSection("settings") as POCO;

    return section;
}

In debug, I can see that _configuration does contain the settings but my "section" is simply returned as null.

I'm aware that I could try to set up the Poco to be created in the startup, and then injected as a dependency, but due to the setup, I'd rather do it in separate classes from the injected IConfiguration, if possible.

My appsettings.json has these values:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",

  "settings": {
    "Username": "user",
    "Password": "password",
    "Domain": "example.com",
    "URL": "http://service.example.com"
  }

}

My poco class looks like this:

public class POCO
{
    public string URL { get; set; }
    public string Username { get; set; }
    public SecureString Password { get; set; }
    public string Domain { get; set; }
}
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
PeaceDealer
  • 977
  • 2
  • 9
  • 20

2 Answers2

9

You need to use:

var section = _configuration.GetSection("settings").Get<POCO>();

What's returned from GetSection is just a dictionary of strings. You cannot cast that to your POCO class.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I don't have an .Get option after GetSelection. – PeaceDealer Jan 31 '19 at 13:37
  • It's in the `Microsoft.Extensions.Configuration` namespace. I don't see how you do not already have a using for that, but if you don't, add one. Otherwise, there's something seriously wrong with your project. – Chris Pratt Jan 31 '19 at 13:43
  • Can confirm that its `using Microsoft.Extensions.Configuration`. The class libary targets .net core 2.2 – PeaceDealer Jan 31 '19 at 13:46
  • Don't know what to tell you. It's there: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder.get?view=aspnetcore-2.2. Make sure that your project is building correctly. – Chris Pratt Jan 31 '19 at 13:50
  • Okay, turns out I was missing the .Binding package. Thank you! – PeaceDealer Jan 31 '19 at 13:57
  • Interesting. That's not something you should need to explicitly reference. Should be covered in a reference to the `Microsoft.Extensions.Configuration` package or `Microsoft.AspNetCore.App` package. – Chris Pratt Jan 31 '19 at 13:58
  • Actually, looks like it's only included by `Microsoft.AspNetCore.App`. Are you using this in a class library or something? If it's an ASP.NET Core app, then you should only be referencing `Microsoft.AspNetCore.App`. – Chris Pratt Jan 31 '19 at 14:02
  • 1
    After adding the nuget package `Microsoft.Extenions.Configurations.Binding` it worked – PeaceDealer Jan 31 '19 at 14:25
  • I know. My point is that unless you're doing this in a class library or some other project where you cannot reference the `Microsoft.AspNetCore.App` metapackage, that should not be necessary. If you have an ASP.NET Core web application project that is *not* referencing `Microsoft.AspNetCore.App`, that is something you need to fix. (Unless you're targeting .NET Framework, in which case you have to reference `Microsoft.AspNetCore` and `Microsoft.AspNetCore.Mvc` instead.) – Chris Pratt Jan 31 '19 at 14:30
6

IConfiguration and IConfigurationSection have an extension method Bind for this purpose:

var poco = new POCO();
_configuration.GetSection("settings").Bind(poco);

Or just Get

var poco = _configuration.GetSection("settings").Get(typeof(POCO));

Or generic Get

var poco = _configuration.GetSection("settings").Get<POCO>();
Zabavsky
  • 13,340
  • 8
  • 54
  • 79