Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting System.Configuration to .NET Core via POCOs?

Tags:

c#

.net

.net-core

We've got a .NET Framework .dll that we're porting to .NET Core. Currently we're inheriting from ConfigurationElement and ConfigurationSection from System.Configuration to create custom configuration sections in the app.config (or it's .NET Core equivalent)

Questions:

  1. It appears the .NET Core way is Microsoft.Extensions.Configuration. Is that correct? Because it lives on ASP.NET Core's github project instead of .NET Core's github project. We have no ASP parts.

  2. If so, any .NET Core examples on creating and loading custom configuration sections not relying on startup.cs ? Ideally we'd like to read from a text source (XML or JSON) directly into a POCO object graph for strongly typed benefits.

  3. With .NET Core 2.0, will there be any support for the traditional ConfigurationElement and ConfigurationSection negating the need for any such porting efforts to begin with ? Reason I ask is the .NET Core 2.0 Roadmap says

    .NET Core gain over 5,000 APIs from .NET Framework as part of this work making it a broader platform.

like image 501
DeepSpace101 Avatar asked Apr 20 '17 05:04

DeepSpace101


People also ask

How do I use IConfiguration in .NET core?

Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.

What is configure in .NET core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json. Environment variables.

What is configuration Manager in asp net?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.


1 Answers

I am not aware of app.config and System.Configuration support in .NET Core. Probably, no, but that's just a guess. You can setup configuration for .NET Core application in Main method:

class Program
{
    static void Main(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        var poco = new Poco();
        configuration.Bind(poco);

        Console.WriteLine(poco);
        Console.ReadKey();
    }
}

class Poco
{
    public bool Enabled { get; set; }
    public Sort Sort { get; set; }

    public override string ToString()
    {
        return $"Enabled={Enabled}, SortOrder={Sort.Order}";
    }
}

class Sort
{
    public int Order { get; set; }
}

appsettings.json is following:

{
  "enabled": true,
  "sort": {
    "order": 2
  }
}

Outputs:

Enabled=True, SortOrder=2

You need to reference Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder packages.

No dependency on ASP.NET Core.

Microsoft.Extensions.Configuration is quite extensible, it can use different settings providers like environment variables, command line arguments, etc. So it is possible to implement custom provider for ConfigurationSection-like configuration if needed.

Based on this comment they are not going to bring System.Configuration to NetStandard 2.0.

like image 95
Andrii Litvinov Avatar answered Sep 30 '22 15:09

Andrii Litvinov