System.Configuration.ConfigurationManager vs Microsoft.Extensions.Configuration
S

1

7

I am working on a class library(.NET Standard 2.0) CL, which will be used by some of our legacy projects(.NET Framework 4.6.1) LP and many of our new implementations(.NET Core 3.1) NI.

My class library CL needs some of the configuration settings but when it is being used by new implementations NI, CL receives Microsoft.Extensions.Configuration.IConfiguration, as NI has appSettings.json.

And when used from legacy projects LP, CL receives System.Configuration.ConfigurationManager.AppSettings as LP has either of web.config or app.config.

Is there any elegant way to read configuration from both types of projects either .NET Framework or .NET Core? I don't want to read the configuration in projects like LP or NI who is consuming my library CL as that configuration is of no use to them.

private static void LoadConfiguration() // called from constructor
{
  string receivedFromNETCore = configuration["DataMappingXml"]; 
  // Microsoft.Extensions.Configuration.IConfiguration    
  string receivedFromNETFramework = 
  ConfigurationManager.AppSettings["DataMappingXml"];
    
  // TODO: LOAD configuration and process.
}
Scandinavian answered 28/4, 2021 at 4:33 Comment(0)
J
1

The recommended way to do fix this is to replace all of the System.Configuration.ConfigurationManager references with Microsoft.Extensions.Configuration references and migrate from ASP.NET to ASP.NET Core. Article from Microsoft about migrating: Migrate from ASP.NET to ASP.NET Core

If migration is not possible for you, you can try platform multi-targeting: Migrating from .NET Framework to .NET Core

Jehanna answered 14/12, 2021 at 1:0 Comment(1)
The link is deadBiramous

© 2022 - 2024 — McMap. All rights reserved.