1

I want to create multiple appsettings.<environment>.json files in my .NET microservice, and when I run my app I want to specify the environment profiles to use and have them apply the appsettings in the order specified.

For example, i'd like to have appsettings.Development.json to have my default local-development properties for running the app, but I'd also like to have appsettings.Hsqldb.json that would override the DB properties in appsettings.Development.json to be an hsql database.

I've got the following launch.json file in vs Code:

{
    "configurations": [
    {
        "name": "C#: App Debug",
        "type": "coreclr",
        "request": "launch",
        "program": "${workspaceFolder}/API/API.csproj",
        "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
    }
    ]
}

And what I'd like to do is "ASPNETCORE_ENVIRONMENT": "Development,Hsqldb". Then it would apply the properties in Development first and the Hsqldb properties overtop after. This doesn't seem possible though.

Is it possible to set two environments at the same time? What would be a good way to handle this?

13
  • For some context, I'm just learning .NET recently, and have been a SpringBoot programmer for a long while. In SpringBoot we can set the running profiles like this and have multiple application-<profile>.properties files. I'm looking for an equivalent feature in .NET Commented Apr 28 at 20:22
  • I think looking at the "Use multiple environments in ASP.NET Core" would mostly solve your problem. Commented Apr 28 at 20:41
  • this answer should help you. I'd use a dedicated EnvironmentName env variable to pass this additional info. Also ordering matters a lot as you will invariably soon find out. So I'd put your appsettings.Hsqldb last but before EnvVariables Commented Apr 28 at 20:44
  • @Brickit Reading through that, it looks like it's applying one profile OR the other, not two at the same time (ie: Development OR Staging). I want it to apply Development and then Staging over top Commented Apr 28 at 20:44
  • 1
    Then I think you should utilize the ASPNETCORE_ENVIRONMENT variable in conjunction with various JSON configuration files and the launchSettings.json file in your project. Commented Apr 28 at 20:48

1 Answer 1

1

Going off of this answer, you could use a custom environment variable (that you specify in launchSettings.json), e.g. CustomSettings that will hold a comma separated list of additional appsetings.xxx.json files to load in addition. Last one wins.

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.Sources.Clear();
// restore default order .NET6+
var env = builder.Environment;
builder.Configuration
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

// plug the CustomSettings after normal appsettings, 
// but before UserSecrets, Env Variables and Command Line arguments
var additionalSettings = Environment.GetEnvironmentVariable("CustomSettings")?
    .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ?? Array.Empty<string>();

foreach (var setting in additionalSettings)
{
    // on Linux case matters ->
    // i.e. CustomSettings = "something" will fail with file
    // appsettings.Something.json
    builder.Configuration.AddJsonFile($"appsettings.{setting}.json",
        optional: false,
        // optional:false to get fail-fast exception here
        // i.e. "Custom1,Custom2" -> don't have the custom2 file - exception
        reloadOnChange: true);
}

// user secrets -> from .net BCL source
if (env.IsDevelopment() && env.ApplicationName is { Length: > 0 })
{
    try
    {
        var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
        builder.Configuration.AddUserSecrets(appAssembly, optional: true, reloadOnChange: true);
    }
    catch (FileNotFoundException)
    {
    }
}

builder.Configuration
    .AddEnvironmentVariables();

if (args is { Length: > 0 })
{
    builder.Configuration.AddCommandLine(args);
}

I'd advise against stacking standard environment names in the new CustomSettings variable and use more feature-like names like Hsqldb.

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

Comments

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.