4

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?

1

2 Answers 2

2

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

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

1 Comment

That's all in learn.microsoft.com/en-us/aspnet/core/fundamentals/environments set ASPNETCORE_ENVIRONMENT=Staging only works for the current command shell
0

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;
}

3 Comments

Interesting, could you explain it a bit more though?
From what I can tell this seems to only work with dotnet run commands.
I don't think it only works with dotnet run command. For more information, you can read this doc: learn.microsoft.com/en-us/aspnet/core/fundamentals/…

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.