ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗? [英] Is ConfigurationManager.AppSettings available in .NET Core 2.0?

查看:54
本文介绍了ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以从我的配置文件中读取设置,如下所示:

I've got a method that reads settings from my config file like this:

var value = ConfigurationManager.AppSettings[key];

仅在面向 .NET Standard 2.0 时编译良好.

It compiles fine when targeting .NET Standard 2.0 only.

现在我需要多个目标,所以我更新了我的项目文件:

Now I need multiple targets, so I updated my project file with:

<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>

但是现在,netcoreapp2.0 的编译失败并显示以下错误消息:

But now, the compilation fails for netcoreapp2.0 with the following error message:

错误 CS0103 当前上下文 (netcoreapp2.0) 中不存在名称ConfigurationManager"

Error CS0103 The name 'ConfigurationManager' does not exist in the current context (netcoreapp2.0)

另外,我创建了一个新的 .NET Core 2.0 控制台应用程序(这次只针对 .NET Core 2.0),但同样在命名空间 System.Configuration 下似乎没有 ConfigurationManager.

Separately, I created a new .NET Core 2.0 console application (only targeting .NET Core 2.0 this time), but likewise there seems to be no ConfigurationManager under the namespace System.Configuration.

我很困惑,因为它在 .NET Standard 2.0 下可用,所以我希望它在 .NET Core 2.0 中可用,因为 .NET Core 2.0 符合 .NET Standard 2.0.

I'm quite confused because it's available under .NET Standard 2.0, so I would expect it to be available in .NET Core 2.0, as .NET Core 2.0 is .NET Standard 2.0 compliant.

我错过了什么?

推荐答案

是的,ConfigurationManager.AppSettings 在引用 NuGet 包后在 .NET Core 2.0 中可用 System.Configuration.ConfigurationManager代码>.

Yes, ConfigurationManager.AppSettings is available in .NET Core 2.0 after referencing NuGet package System.Configuration.ConfigurationManager.

感谢@JeroenMostert 为我提供了解决方案.

Credits goes to @JeroenMostert for giving me the solution.

这篇关于ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆