C# ConfigurationManager.AppSettings是否在.NET Core 2.0中可用?

C# ConfigurationManager.AppSettings是否在.NET Core 2.0中可用?,c#,.net-core,app-config,.net-standard,C#,.net Core,App Config,.net Standard,我有一个从配置文件中读取设置的方法,如下所示: var value = ConfigurationManager.AppSettings[key]; 当只针对.NET标准2.0时,它可以很好地编译 现在我需要多个目标,所以我更新了我的项目文件: <TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks> 另外,我创建了一个新的.NET Core 2.0控制台应用程序(这次只针对.NE

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

var value = ConfigurationManager.AppSettings[key];
当只针对.NET标准2.0时,它可以很好地编译

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

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

我很困惑,因为它在.NET标准2.0下可用,所以我希望它在.NET Core 2.0中可用,因为.NET Core 2.0与.NET标准2.0兼容


我错过了什么

是,
ConfigurationManager.AppSettings
在参考NuGet软件包
System.ConfigurationManager
后在.NET Core 2.0中可用


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

完成软件包设置后,您需要创建app.config或web.config并添加以下内容:

<configuration>
  <appSettings>
    <add key="key" value="value"/>
  </appSettings>
</configuration>
public class TargetClassController : ControllerBase
{
    private readonly IConfiguration _config;

    public TargetClassController(IConfiguration config)
    {
        _config = config;
    }

    [HttpGet("{id:int}")]
    public async Task<ActionResult<DTOResponse>> Get(int id)
    {
        var config = _config["YourKeySection:key"];
    }
}

我从Nuget将
System.Configuration.ConfigurationManager安装到.net core 2.2应用程序中

然后我使用System.Configuration引用

接下来,我换了衣服

WebConfigurationManager.AppSettings

to ..

ConfigurationManager.AppSettings
到目前为止,我相信这是正确的<代码>4.5.0是.net core 2.2的典型版本


我对此没有任何问题。

最新的指南如下:(来源)

使用:

System.Environment.GetEnvironmentVariable(名称,EnvironmentVariableTarget.Process)

从文档中:

公共静态类EnvironmentVariablesExample
{
[函数名(“GetEnvironmentVariables”)]
公共静态无效运行([TimerTrigger(“0*/5****”)TimerInfo myTimer,ILogger日志)
{
log.LogInformation($“C#计时器触发器函数在:{DateTime.Now}执行”);
log.LogInformation(GetEnvironmentVariable(“AzureWebJobsStorage”);
log.LogInformation(GetEnvironmentVariable(“网站名称”);
}
公共静态字符串GetEnvironmentVariable(字符串名称)
{
返回名称+“:”+
System.Environment.GetEnvironmentVariable(名称,EnvironmentVariableTarget.Process);
}
}
在本地开发和在Azure中运行时,都可以从环境变量读取应用程序设置。在本地开发时,应用程序设置来自local.settings.json文件中的
Values
集合。在本地和Azure环境中,
GetEnvironmentVariable(“属性”是获取应用程序设置值的替代API,但我们建议您使用
GetEnvironmentVariable
,如下所示


您可以使用配置来解决此问题

Ex(Startup.cs):

在此实现之后,您可以通过DI传递给控制器

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();

    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        var microserviceName = Configuration["microserviceName"];

       services.AddSingleton(Configuration);

       ...
    }

我知道这有点晚了,但也许有人正在寻找一种简单的方法来访问.net核心应用程序中的应用程序设置。 在API构造函数中添加以下内容:

<configuration>
  <appSettings>
    <add key="key" value="value"/>
  </appSettings>
</configuration>
public class TargetClassController : ControllerBase
{
    private readonly IConfiguration _config;

    public TargetClassController(IConfiguration config)
    {
        _config = config;
    }

    [HttpGet("{id:int}")]
    public async Task<ActionResult<DTOResponse>> Get(int id)
    {
        var config = _config["YourKeySection:key"];
    }
}
公共类TargetClassController:ControllerBase
{
专用只读IConfiguration\u config;
公共TargetClassController(IConfiguration配置)
{
_config=config;
}
[HttpGet(“{id:int}”)]
公共异步任务Get(int-id)
{
var config=_config[“YourKeySection:key”];
}
}

您可能没有找到。(请注意,.NET标准目标同时包含.NET和.NET核心,因此不需要单独构建它们。)感谢@JeroenMostert,添加NuGet软件包System.Configuration.ConfigurationManager解决了问题。现在,这可能是一个单独的问题,但如果需要添加软件包来填充缺失的位,.NET Core 2.0如何被视为符合.NET Standard 2.0?“.NET Standard 2.0兼容”的意思是”如果您将其构建为目标.NET标准2.0,它将在.NET Core 2.0(以及其他平台)上运行”。这并不意味着“如果您将其构建为目标.NET Core 2.0,则所有的.NET标准2.0 API都将可用,无需进一步操作。”“。如果您按照.NET标准2.0构建,但它无法在.NET Core上运行,那么您有理由抱怨,但我认为这一切都会成功。(不过,我还没有对它进行测试。)@Alexanssanséau NuGet软件包不是多边形填充。在开始使用.NET Core时,Microsoft决定让API选择加入,这意味着您的应用程序占用的空间更小。我建议您花些时间观看Immo Landwerth在.NET Standard()上创建的视频-他是.NET标准团队中的PM:
仅针对.NET标准2.0时编译良好
-这不可能是正确的,因为
ConfigurationManager
不是.NET标准的一部分(到目前为止,这在v.2.1中是正确的)你能从你的配置文件中发布代码吗?我想知道我是如何在.NET Core 2中设置全局变量的。0@egmfrs,您介意重新措辞吗?全局变量将以类的静态属性的形式存在。配置文件包含应用程序设置是它们的常用格式:
@AlexSanséau,我正在尝试e我在哪里可以设置AppSettings的值。web.config或AppSettings.json不起作用。你能举个例子说明在哪里设置AppSettings吗?谢谢。@Jandutschl,它应该进入你的web.config或app.config的AppSettings部分,就像你在传统的.NET项目中所做的那样。我有点困惑。这确实列出了
.NET Framework 4.6
作为依赖项。这是否意味着我的“.NET Core”项目不再是纯粹的
Core
项目?迁移的一个简单修复方法。其他一些没有那么有趣:/你也创建了一个web.config文件吗?它不适合我(.NET Core 3.1)。它没有任何设置数据,如果access@Arteny-我从未用3.1测试过它-我安装了3.1并在家里玩过它-但我当前的客户端不想很快升级。对于任何尝试过它的人,你都会得到语法