18

We have a .NET Core console application.

static void Main(string[] args)
{
    var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    var builder = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.json", true, true)
        .AddJsonFile($"appsettings.{environmentName}.json", true, true)
        .AddEnvironmentVariables();
    var configuration = builder.Build();
}

environmentName is always null. How to set ASPNETCORE_ENVIRONMENT in App.config file ?

0

4 Answers 4

18

The OP specified that it's a console app. Starting from .NET Core 3.0, you will have to use DOTNET_ENVIRONMENT instead of ASPNETCORE_ENVIRONMENT for console apps. For example, the launchsettings.json will be this for Development environment:

"ConsoleApplication - Development": {
  "commandName": "Project",
  "environmentVariables": {
    "DOTNET_ENVIRONMENT": "Development"
  }
}

If you use ASPNETCORE_ENVIRONMENT, your console host will default to production environment.

Even though console apps have no launchsettings.json file in the Properties folder, as soon as you change a property in the console project's properties window, visual studio will automatically create a Properties folder and add a launchsettings.json file.

Sign up to request clarification or add additional context in comments.

2 Comments

I had to modify the launch profile under debug - general before this file appeared.
And if anyone needs to access this environment variable, you can do so with the following: Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")
5

You need to set the environment variable ASPNETCORE_ENVIRONMENT first before you run your application or use a default value if environment variable ASPNETCORE_ENVIRONMENT is null.

The environment variable ASPNETCORE_ENVIRONMENT has the default value Production in a aspnet-core application, because this is ensured by runtime when variable is not set.

But you have a dotnet application (console) and the variable is not initialized by default.

WINDOWS

C:\> set ASPNETCORE_ENVIRONMENT=Development
C:\> dotnet run ...

UNIX

$ export ASPNETCORE_ENVIRONMENT=Development
$ dotnet run ...

1 Comment

Because this is a .NET Console app, there is no ASPNETCORE_ENVIRONMENT. It will have to use DOTNET_ENVIRONEMENT. Otherwise like you said, the console app will default the environment to production.
3
+25

Asp.net core applications take from the launchsettings.json file when developing in visual studio and when you publish the app will look at the web.config or app.config file. So keep this in mind if you are using visual studio. I'll show you how to set that up aswell. (NOTE: I'm using asp.net core MVC which might be different)

Your launchsettings.json file is located under properties:

enter image description here

And should look something like this

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:58251",
      "sslPort": 44362
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Workorders2": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

When you publish, the app will use your app.config settings. Set it up like this:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT")" value="Production" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

Then all you need to do is get the environment variable like this:

Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")");

1 Comment

I have mentioned its a console application. Not a ASP.NET Web Application
0
      string value;
      bool toDelete = false;

      // Check whether the environment variable exists.
      value = Environment.GetEnvironmentVariable("Test1");
      // If necessary, create it.
      if (value == null)
      {
         Environment.SetEnvironmentVariable("Test1", "Value1");
         toDelete = true;

         // Now retrieve it.
         value = Environment.GetEnvironmentVariable("Test1");
      }
      // Display the value.
      Console.WriteLine($"Test1: {value}\n");

      // Confirm that the value can only be retrieved from the process
      // environment block if running on a Windows system.
      if (Environment.OSVersion.Platform == PlatformID.Win32NT)
      {
         Console.WriteLine("Attempting to retrieve Test1 from:");
         foreach (EnvironmentVariableTarget enumValue in
                           Enum.GetValues(typeof(EnvironmentVariableTarget))) {
            value = Environment.GetEnvironmentVariable("Test1", enumValue);
            Console.WriteLine($"   {enumValue}: {(value != null ? "found" : "not found")}");
         }
         Console.WriteLine();
      }

      // If we've created it, now delete it.
      if (toDelete) {
         Environment.SetEnvironmentVariable("Test1", null);
         // Confirm the deletion.
         if (Environment.GetEnvironmentVariable("Test1") == null)
            Console.WriteLine("Test1 has been deleted.");
      }
   }
}
// The example displays the following output if run on a Windows system:
//      Test1: Value1
//
//      Attempting to retrieve Test1 from:
//         Process: found
//         User: not found
//         Machine: not found
//
//      Test1 has been deleted.
//
// The example displays the following output if run on a Unix-based system:
//      Test1: Value1
//
//      Test1 has been deleted.

2 Comments

Its a Console application
@George: Everything about this answer should work with a Console application. Am I missing some nuance here in terms of your response to Alex?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.