System.Configuration.Configuration an object reference is required

Viewed 2375

I'm tearing my hair out over a simple problem. I had some settings in my .NET Core 3 app which I thought would be best to move to my appsettings.json file. I followed this guide to do so: https://www.c-sharpcorner.com/article/reading-values-from-appsettings-json-in-asp-net-core/

Further down in the guide, there is a section about how to dependency inject part of the config, which I thought would be a good idea to follow. It says to add the following code to my Startup.cs file in the ConfigureServices method.

services.Configure<ConfigurationProps>(Configuration.GetSection("ApplicationConfiguration"));

When I do this, at first Configuration is unknown, and I cannot import it from any known extension/library. Looking at the recommendations, I can see a NuGet package System.Configuration.ConfigurationManager is available. I therefore tried to install that to see if it would help me. Unfortunately, after doing that, I get the following error from the Configuration object:

An object reference is required for the non-static-field, method, or property 'Configuration.GetSection(string)'

From the guide I've been following, and other documentation I've been able to find online, I don't need to instantiate the Configuration class anywhere.

So, how do I inject my configuration into the services.Configure method following this approach?

EDIT: Added files

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationConfiguration": {
    "StartingPort": 13000,
    "NumberOfConnections": 1
  }
}

ConfigurationProps:

namespace ExternalGateway.Models
{
    public class ConfigurationProps
    {
        public int StartingPort { get; set; }
        public int NumberOfConnections { get; set; }
    }
}
3 Answers

I'm going to try to make what I was saying a little easier to read based on your appsettings.json file and your ConfigurationProps class:

In your startup.cs file, make sure you have:

using Microsoft.Extensions.Configuration;

    public IConfiguration Configuration { get; }

in your startup.cs file. Without that, Configuration.GetSection is unknown.

Modify your class to:

namespace ExternalGateway.Models
    {
        public class ConfigurationProps
        {
            public const string ConfigurationProps = "ApplicationConfiguration";

            public int StartingPort { get; set; }
            public int NumberOfConnections { get; set; }
        }
    }

Then your line in startup.cs to get the config will look like this:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ConfigurationProps>(Configuration.GetSection(ConfigurationProps.ApplicationConfiguration));
}

Then your dependency injection into your your class looks like this:

using Microsoft.Extensions.Options;

    public class MyClass
    {
        private readonly int _startingPort;
        private readonly int _numberOfConnections;

        public MyClass(IOptions<ConfigurationProps> configurationProps)
        {
            _startingPort = configurationProps.Value.StartingPort;
            _numberOfConnections = configurationProps.Value.NumberOfConnections;
        }

Barring any typos here, this should accomplish what you're trying to do using the options pattern in .NET Core.

I think the guide that you mentioned is using .netcore 2.0. With .netcore 3.1, you only need to include "IConfiguration configuration" in the constructor of the controller, then you can access to your appSettings.json by configuration["ApplicationConfiguration:StartingPort"], like below sample:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;


namespace ConfigTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class YourController : ControllerBase
    {        
        private readonly IConfiguration _configuration;

        public YourController(IConfiguration configuration)
        {
            _configuration = configuration;           
        }

        [HttpGet]
        public ActionResult Get()
        {
            return Ok(_configuration["ApplicationConfiguration:StartingPort"]);
        }
    }
}


What you're trying to do looks a lot like the Options pattern. There is a very straight-forward article on it here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1

You don't need the NuGet package to use it. You would just have to change your Configuration.GetSection statement and make sure your appsettings.json and your "MySettingsModel" are structured correctly.

If the article doesn't fix this for you, can you post your model and appsettings.json?

Related