Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core console application, how to configure appSettings per environment?

I have a .NET Core 1.0.0 console application and two environments. I need to be able to use appSettings.dev.json and appSettings.test.json based on environment variables I set at run time. This seems to be quite straight forward for ASP.NET Core web applications, via dependency injection and IHostingEnvironment and the EnvironmentName env. variable, however how should I wire things up for the console application (besides writing my own custom code that uses Microsoft.Framework.Configuration.EnvironmentVariables)?

Thank you.

like image 241
user2916547 Avatar asked Sep 19 '16 12:09

user2916547


People also ask

How do you set an environment variable in the NET Core console app?

With all these packages installed, I could initialize the ConfigurationBuilder with a subset of the default ASP.NET Core setup: var environment = Environment. GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() . AddJsonFile($"appsettings.

Can I add more than two Appsettings json files in dotnet core?

Of course, we can add and use multiple appsettings. json files in ASP.NET Core project. To configure and read data from your custom json files, you can refer to the following code snippet. Host.


2 Answers

This is how we do it in our .netcore console app. The key here is to include the right dependencies on your project namely (may be not all, check based on your needs) and copy to output the appSetting.json as part of your buildoptions

  {     "buildOptions": {     "emitEntryPoint": true,     "copyToOutput": {        "include": [        "appsettings*.json",        "App*.config"                  ]           } }, 
using Microsoft.Extensions.Configuration; namespace MyApp {     public static void Main(string[] args)     {         var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");                  var builder = new ConfigurationBuilder()             .AddJsonFile($"appsettings.json", true, true)             .AddJsonFile($"appsettings.{environmentName}.json", true, true)             .AddEnvironmentVariables();         var configuration = builder.Build();         var myConnString= configuration.GetConnectionString("SQLConn");     } } 
like image 174
Jaya Avatar answered Oct 15 '22 06:10

Jaya


For those who are using .NET Core version 2.1.0+ and Microsoft.Extensions.Hosting to host your console app, you can use the following code (according to the Feiyu Zhou's answer in another thread):

var hostBuilder = new HostBuilder()     .ConfigureHostConfiguration(config =>     {         if (args != null)         {             // enviroment from command line             // e.g.: dotnet run --environment "Staging"             config.AddCommandLine(args);         }     })     .ConfigureAppConfiguration((context, builder) =>     {         builder.SetBasePath(AppContext.BaseDirectory)             .AddJsonFile("appsettings.json", optional: false)             .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);     }) 
like image 26
victorm1710 Avatar answered Oct 15 '22 07:10

victorm1710