代码之家  ›  专栏  ›  技术社区  ›  BvdVen dario_ramos

在容器中的core控制台应用程序中获取ASPNETCORE_环境

  •  0
  • BvdVen dario_ramos  · 技术社区  · 6 年前

    我有一个ASP.NET核心控制台应用程序,我在Kubernetes上的一个容器中运行。在deployment.yaml中,我设置了环境变量:

    env:
       - name: "ASPNETCORE_ENVIRONMENT"
         value: "Development"
    

    static void Main(string[] args)
     {
                Console.WriteLine("Env: " + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
     }
    

    但是ASPNETCORE_环境是空的,我如何才能得到配置的环境变量?我在一个核心webapi项目中使用相同的步骤,在那里我得到如下变量:

    public Startup(IHostingEnvironment env)
    {
         env.EnvironmentName
    }
    

    0 回复  |  直到 6 年前
        1
  •  1
  •   maveonair    6 年前

    我们使用此模板在Kubernetes群集中运行一个ASP.NET核心应用程序:

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      namespace: <namespace>
      name: <application name>
    spec:
      replicas: 1
      minReadySeconds: 15
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 0
      template:
        metadata:
          labels:
            app: <app-label>
        spec:
          containers:
            - name: <container-name>
              image: <image-name>
              ports:
                - containerPort: 80
              env:
                - name: ASPNETCORE_ENVIRONMENT
                  value: "Release"
    
        2
  •  0
  •   BvdVen dario_ramos    6 年前

    static void Main(string[] args)
    {
        string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    }
    
    推荐文章