Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using appsettings.ENV.json in .NET Core Console App

I'm building a console app to do some database work for me and want to set up different appsettings files to store the appropriate connection strings per environment. I have something this working in my Main function at the moment:

static void Main(string[] args)
{
            IConfiguration config = new ConfigurationBuilder()
              .AddJsonFile("appsettings.json", true, true)
              .Build();

            var dbconnection = config["db"];
}

But while this is working, it's just one global appsettings file. I'd like to be able to create different appsettings for each environment (e.g. appsettings.Dev.json, appsettings.Staging.json, etc.), but I can't figure out how to feed whatever selection I have in the configuration manager when I run the app to the addjsonfile string.

For some additional context, I'd like to feed this into my dbcontext later in the app if that affects anything.

like image 801
Paul DeVito Avatar asked Dec 31 '22 02:12

Paul DeVito


2 Answers

The other answers here were helpful, but I wanted to expand on exactly what I did for others that come across this.

The first thing you'll want to do is to go into your launch settings.json and add something like this:

{
  "profiles": {
    "DataMigration (Local)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Local"
      }
    },
    "DataMigration (Dev)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Dev"
      }
    }
  }
}

This will give you access to an environment variable depending on which profile you launch your project with (note the configuration manager has no impact here).

Then you can create different appsetting.json files. For example, I made an appsettings.Local.json and a appsettings.Dev.json file.

appsettings.Local.json

{
  "db": "connectionstringhere"
}

and

appsettings.Dev.json

{
  "db": "connectionstringhere"
}

Now, you can then access the environment variable that you run your app with like so:

static void Main(string[] args)
{
    var myEnv = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    IConfiguration config = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.{myEnv}.json", false)
        .Build();

    var dbconnection = config["db"];
}

To take it a step further, instead of doing it here, I made by db context flexible like so:

public class ShoppingDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var myEnv = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        if(myEnv != null)
        {
            IConfiguration config = new ConfigurationBuilder()
                 .AddJsonFile($"appsettings.{myEnv}.json", false)
                 .Build();

            var sl = config[$"db"];
           optionsBuilder.UseSqlServer(sl);
        }
        else
        {
            // we are testing and want local for sure
            IConfiguration config = new ConfigurationBuilder()
                 .AddJsonFile($"appsettings.Local.json", false)
                 .Build();

            var sl = config[$"db"];
            optionsBuilder.UseSqlServer(sl);
        }
    }

    public DbSet<MyTable> MyTables { get; set; }
}
like image 147
Paul DeVito Avatar answered Jan 12 '23 01:01

Paul DeVito


You can have as many configuration files as you want just add a line of code to your configuration builder as below :

IConfiguration config = new ConfigurationBuilder()
          .AddJsonFile("appsettings.json", false)
          .AddJsonFile($"appsettings.{environmentName}.json", true)
          .Build();

You need also to configure your environement name for your application as shwon below :

enter image description here

like image 43
sayah imad Avatar answered Jan 12 '23 01:01

sayah imad