在 .NET Core 中将应用设置与环境变量合并 [英] Merging appsettings with environment variables in .NET Core

查看:37
本文介绍了在 .NET Core 中将应用设置与环境变量合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Docker(在 Kubernetes 中)运行一个 .NET Core 应用程序,将环境变量传递给 Docker 容器并在我的应用程序中使用它们.

I am running a .NET Core app in Docker (in Kubernetes), passing environment variables to the Docker container and using them in my app.

在我的 .NET Core 应用程序中,我有以下 C# 类:

In my .NET Core app I have the following C# class:

public class EnvironmentConfiguration
{
    public string EXAMPLE_SETTING { get; set; }
    public string MY_SETTING_2 { get; set; }
}

我这样设置我的appsettings:

config.
    AddJsonFile("appsettings.json").
    AddJsonFile($"appsettings.docker.json", true).
    AddEnvironmentVariables();  

DI 设置:

services.Configure<EnvironmentConfiguration>(Configuration);

在我的控制器中,我这样使用它:

And in my Controller I use it as such:

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/my")]
public class MyController : Controller
{
    private readonly IOptions<EnvironmentConfiguration> _environmentConfiguration;

    public MyController(IOptions<EnvironmentConfiguration> environmentConfiguration)
    {
        _environmentConfiguration = environmentConfiguration;
    }
}       

我运行 docker:

I run docker:

docker run -p 4000:5000 --env-file=myvariables

文件 myvariables 看起来像这样:

The file myvariables looks like this:

EXAMPLE_SETTING=example!!!
MY_SETTING_2=my-setting-2!!!!

这有效.我可以使用我的 _environmentConfiguration 并查看我的变量是否已设置.

This works. I can use my _environmentConfiguration and see that my variables are set.

但是...我想将环境变量与 appsettings 合并,以便在找不到环境变量时将 appsettings 中的值用作后备.以某种方式合并这两行:

However... I would like to merge environment variables with appsettings so that the values from appsettings are used as fallback when environment variables are not found. Somehow merging these two lines:

services.Configure<EnvironmentConfiguration>(settings => Configuration.GetSection("EnvironmentConfiguration").Bind(settings));
services.Configure<EnvironmentConfiguration>(Configuration);

这有什么可能吗?

我的后备计划是从 EnvironmentConfiguration 类继承并使用单独的 DI 注入两个单独的配置,然后在代码中手动"合并它们,但这种解决方案是不可取的.

My fallback plan is to inherit from the EnvironmentConfiguration class and use a separate DI to have two separate configurations injected and then merge them "manually" in code but this solution is undesirable.

推荐答案

config.
    AddJsonFile("appsettings.json").
    AddJsonFile("appsettings.docker.json", true).
    AddEnvironmentVariables();

实际上足以使用环境变量覆盖 appsettings 值.

is actually enough to override appsettings values using environment variables.

假设您的 appsettings.json 文件中有以下内容;

Let's say you have the following in your appsettings.json file;

{
  "Logging": {
      "Level": "Debug"
  }
}

您可以通过将名为 Logging:Level 的环境变量设置为您的首选项值来覆盖 Logging.Level 的值.

you can override value of Logging.Level by setting the environment variable named Logging:Level to the value of your preference.

请注意 : 用于指定环境变量键中的嵌套属性.

Be aware that : is used to specify nested properties in environment variable keys.

另请注意:来自 文档;

如果您系统上的环境变量名称中不能使用冒号 (:),请将冒号 (:) 替换为双下划线 (__).

这篇关于在 .NET Core 中将应用设置与环境变量合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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