0

While I was having issues with a publish profiles, I decided to create a small test console app, using .net core 3 and see if my environment variables are getting set and the corresponding appsettings.json file being read from.

So, here goes.

Program.json
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Cmd1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");


            var environmentName =
                Environment.GetEnvironmentVariable("ENVIRONMENT");

            // create service collection
            var services = new ServiceCollection();


            // build config
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", false)
                .AddJsonFile($"appsettings.{environmentName}.json", true)
                .AddEnvironmentVariables()
                .Build();

            // setup config
            services.AddOptions();
            services.Configure<AppSettings>(configuration.GetSection("App"));

            // create service provider
            var serviceProvider = services.BuildServiceProvider();

            var appSettings = serviceProvider.GetService<IOptions<AppSettings>>();

            string env = Environment.GetEnvironmentVariable("Environment");

            Console.WriteLine($" We are looking at {appSettings.Value.TempDirectory} from environment: {env}");
        }
    }
}

Setting Environment for running from Visual Studio enter image description here

appsettings.json

{
  "App": {
    "TempDirectory": "d:\temp"
  }
}

appsettings.Local.json

{
  "App": {
    "TempDirectory": "c:\\temp\\rss-hero\\tmp\\"
  }
}

appsettings.Test.json

{
  "App": {
    "TempDirectory": "d:\test"
  }
}

Now, going to the commandline and running dotnet run, I get enter image description here

If I try to set the environment on the commandline, it doesnt seem to take it. What am I missing? Same sort of problem occurs If I use a publish profile for this console app.

enter image description here

[ Edit 2] After adding the ability to use commandline args,

    if (args != null)
    {
        if (args.Length > 0)
        {
            Environment.SetEnvironmentVariable("Environment", args[1]);
        }
    }

enter image description here

bitshift
  • 6,026
  • 11
  • 44
  • 108
  • When running `dotnet run --environment "Test"`. Arguments `--environment` and `Test` are passed into the `args` parameter of your `Main` method. Thus `args` contains two elements. `args[0]` contains `--environment` and `args[1]` contains `Test` – An0d Oct 25 '19 at 21:03
  • Thanks, I added the nuget package to handle commandline args, then made the changes to set the environment var. See Edit 2 above. Is there not a way to set this during a msbuild publish because other than using commandline args, publish doesnt seem to change this value for console apps, only web apps – bitshift Oct 25 '19 at 21:28
  • 1
    You need to define an environment variable on the target machine. And then access this environment variable by code. Please see https://stackoverflow.com/questions/39573571/net-core-console-application-how-to-configure-appsettings-per-environment – An0d Oct 26 '19 at 05:49
  • Also take a look at https://12factor.net/config. This covers why environment variables must be set on the target and must not be part of the published application. TLDR: applications should pick up configuration from environment and the same binary (not different binaries) should understand production vs development environments. – omajid Oct 28 '19 at 20:07

0 Answers0