Jammer
Jammer

Reputation: 10208

Running .NET Core API Linux Environment Launch Profiles

So I'm trying to get a .NET Core API application running with different environment settings. I've been reading the documentation and as far as I can tell I've followed the instructions. Yet when I run the service outside of VS it blows up due to not being able to find a connection string. I'm clearly missing something basic.

I've setup two profiles in launchSettings.json "Development" and "Staging"

"profiles": {
  "IIS Express": {
    "commandName": "Project",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  },
  "Development": {
    "commandName": "Project",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  },
  "Staging": {
    "commandName": "Project",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Staging"
    }
  }

I have the following configuration files.

appsettings.json
appsettings.Development.json
appsettings.Staging.json

I also have the following code in in my Startup.cs:

var builder = new ConfigurationBuilder()  
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();

Configuration = builder.Build();

When I debug this I can see that .AddJsonFile($"appsettings.{env.EnvironmentName}.json" is loading the expected file. However, when I publish this to a directory and attempt to run it the connectionString value us null.

appsettings.json doesn't contain a value for the connection string. It is in the two environment dependent files, defined like:

"DefaultConnection": {
    "ConnectionString":
        "Server=name;Port=3307;Database=name;User Id=name;Password=name;"
},

Since this all works inside VS I'm sure this is all correct. However when I try to run it, BOOM!

dotnet MyDll.dll --launch-profile Staging

I've just noticed that --launch-profile only works with dotnet run and that when I try to run the application it's looking for a Production.json file. How do I use the various profiles with just the dotnet command, not the dotnet run command?

Upvotes: 4

Views: 4041

Answers (2)

Feiyu Zhou
Feiyu Zhou

Reputation: 4554

In addition to set the environment variables (AddEnvironmentVariables), you can also use command line (AddCommandLine), here's an example:

public static IHostBuilder CreateDefaultBuilder(string[] args)
{
    var hostBuilder = new HostBuilder()
        // ConfigureHostConfiguration is only for IHostingEnvironment currently
        .ConfigureHostConfiguration(config =>
        {
            config.AddEnvironmentVariables();

            if (args != null)
            {
                // e.g.: dotnet run --environment "Staging"
                config.AddCommandLine(args);
            }
        })
        .ConfigureAppConfiguration((context, builder) =>
        {
            var env = context.HostingEnvironment;
            builder.SetBasePath(AppContext.BaseDirectory)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

            if (args != null)
            {
                builder.AddCommandLine(args);
            }
        });

    return hostBuilder;
}

Upvotes: 0

Jammer
Jammer

Reputation: 10208

So, after some more poking around. It seems that you have to set the environment variable on the server, I had mistakenly thought this was some kind of "runtime environment" variable, nope it's a full on OS level environment variable:

LINUX

export ASPNETCORE_ENVIRONMENT=Staging

POWERSHELL

$Env:ASPNETCORE_ENVIRONMENT="Staging"

WINDOWS

set ASPNETCORE_ENVIRONMENT=Staging

Upvotes: 2

Related Questions