'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.
}
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |