【发布时间】:2021-07-21 10:21:09
【问题描述】:
我正在开发一个类库 (.NET Standard 2.0) CL,它将被我们的一些遗留项目 (.NET Framework 4.6.1) LP 和我们的许多新实现 (.NET Core 3.1) NI 使用.
我的类库 CL 需要一些配置设置,但是当它被新的实现 NI 使用时,CL 接收到Microsoft.Extensions.Configuration.IConfiguration,因为 NI 有appSettings.json。
当从遗留项目 LP 中使用时,CL 收到 System.Configuration.ConfigurationManager.AppSettings,因为 LP 有 web.config 或 app.config。
是否有任何优雅的方法可以从 .NET Framework 或 .NET Core 两种类型的项目中读取配置?我不想阅读像 LP 或 NI 这样正在使用我的库 CL 的项目中的配置,因为该配置对他们没有用。
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.
}
【问题讨论】:
标签: .net-core .net-standard-2.0 configurationmanager .net-framework-4.8 microsoft.extensions.configuration