Get ASPNETCORE_ENVIRONMENT in core console app in container

10/4/2019

I've a ASP.NET core console application that I run in a container on Kubernetes. In the deployment.yaml I've set the environment variable:

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

And in the console application I've the following code:

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

But the ASPNETCORE_ENVIRONMENT is empty, how can I get to the configured environment variable? I use the same steps in a core webapi project and there I get the variable as follow:

public Startup(IHostingEnvironment env)
{
     env.EnvironmentName
}

This works in the core webapi but I don't have IHostingEnvironment in the console app.

-- BvdVen
asp.net-core
containers
core
kubernetes

2 Answers

10/7/2019

I've kind of found the 'reason' I guess. I wanted to read the "ASPNETCORE_ENVIRONMENT in the "static Program()" method, there it was empty. But when I do the same in the "void Main(string[] args)" method it is set and it works as expected.

static void Main(string[] args)
{
    string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
}
-- BvdVen
Source: StackOverflow

10/4/2019

We use this template to run a ASP.NET Core App in our Kubernetes cluster:

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"
-- maveonair
Source: StackOverflow