Porting System.Configuration to .NET Core via POCOs?

We have .NET Framework .dllwhich we carry on .NET Core. We are currently inheriting from ConfigurationElementand ConfigurationSectionfrom System.Configurationto create custom configuration sections in app.config(or is it the equivalent of .NET Core)

Questions:

  • It looks like the .NET Core path is Microsoft.Extensions.Configuration. It's right? Because he lives on ASP.NET Core a github project instead of .NET Core github . We have no ASP parts.

  • If so, are there any examples .NET Corefor creating and loading custom configuration sections that do not rely on startup.cs? Ideally, we would like to read from a text source (XML or JSON) directly in the POCO object graph for strongly typed benefits.

  • With .NET Core 2.0, will support traditional ConfigurationElementand ConfigurationSectiondeny the need for any such migration efforts to get started? The reason I'm asking for is that the .NET Core 2.0 Roadmap says

    The .NET Core uses more than 5,000 .NET Framework APIs as part of this work, making it a broader platform.

+4
source share
3 answers

With the removal of dust from the version of .NET Standard 2.0, you can use the usual System.Configurationeven in .NET Core 2.0 on Linux!

Here is an example test:

  • The .NET Standard 2.0 library is created (let's say MyLib.dll)
  • NuGet System.Configuration.ConfigurationManager v4.4.0. , - NetStandard.Library v2.0.0 ( , )
  • #, ConfigurationSection ConfigurationElement, MyLib.dll. , MyClass.cs ConfigurationSection MyAccount.cs ConfigurationElement. , Google -
  • .NET Core 2.0 (, , MyApp.dll). .NET Core .dll, .exe Framework.
  • app.config MyApp . , , # 3 . :
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="myCustomConfig" type="MyNamespace.MyClass, MyLib" />
      </configSections>
      <myCustomConfig>
        <myAccount id="007" />
      </myCustomConfig>
    </configuration>

- , app.config MyApp, MyLib . dotnet restore, Windows (dev) Linux (test).

+1

app.config System.Configuration .NET Core. , , . .NET Core Main:

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:

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

:

Enabled=True, SortOrder=2

Microsoft.Extensions.Configuration.Json Microsoft.Extensions.Configuration.Binder.

ASP.NET Core.

Microsoft.Extensions.Configuration , , , .. , ConfigurationSection -like.

System.Configuration NetStandard 2.0.

+6

Microsoft.Extensions.Configuration ( ), ( , ), System.Configuration .NET Core 2.

Here are the System.Configurationtypes in corefx: https://github.com/dotnet/corefx/tree/master/src/System.Configuration.ConfigurationManager

I can’t tell you that they are fully compatible with tags from .NET. But at least that's what makes us hope)

So, it looks like .NET Core 2 will have this old System.Configurationstuff, but not netstandard2. Probably because MS does not want to share these types among other platforms (Xamarin).

+2
source

Source: https://habr.com/ru/post/1675233/