Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Configuration.ConfigurationManager vs Microsoft.Extensions.Configuration

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.
}
like image 460
user960482 Avatar asked Jul 01 '26 14:07

user960482


1 Answers

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

like image 57
Tyson Gibby Avatar answered Jul 05 '26 16:07

Tyson Gibby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!