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.
You can do this for ASP.Net Core environment variable (ASPNETCORE_ENVIRONMENT): -
Then you can simply use the property
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}
There are two
IHostingEnvironment
interfaces that you should use. One is for ASP.NET Core Applications, the other one is for .NET Core Console applications. You can use this code example for both: