I want to get the name of the current environment (QA/Prod/DEV) in my code, so I can then use that name when calling AddJsonFile
*Note that this code is not for an application, but instead for selenium Unit tests using the test explorer test runner with NUNIT, so I do not have a Startup.cs class or ihostingenvironment that I can use.
I thought to do this, I would need to retrieve the "Hosting:Environment" or "ASPNETCORE_ENVIRONMENT" value inside the launchsettings.json, which I thought should represent the CURRENT Visual Studio process/session/environment. See Environment.GetEnvironmentVariable("Hosting:Environment")
below for how I am retreiving this launchsettings.json value.
public static IConfiguration InitConfiguration()
{
LaunchSettingsFixture LaunchSettings = new LaunchSettingsFixture();
var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment");
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environmentName}.json", true)
.AddEnvironmentVariables();
var configuration = builder.Build();
// Testing to see if the above worked
string url = configuration["url"];
string driverdirectory = configuration["driverdirectory"];
return configuration;
}
In the above code, I am expecting Environment.GetEnvironmentVariable("Hosting:Environment")
to return me a string with a value of "blah" because I selected "blah" in the current process/profile/environment drop down (the drop down next to the Run button). See photo below for my current process/profile selection, and see my json text inside my launchsettings.json file below...
{
"profiles": {
"Wikipedia.UITest": {
"commandName": "Project"
},
"UAT": {
"commandName": "Executable",
"environmentVariables": {
"Hosting:Environment": "UAT"
}
},
"blah": {
"commandName": "Executable",
"environmentVariables": {
"Hosting:Environment": "Development",
"containerName": "Testing"
}
},
"Prod": {
"commandName": "Executable",
"environmentVariables": {
"Hosting:Environment": "prod"
}
}
}
}
Instead Environment.GetEnvironmentVariable("Hosting:Environment")
returns me the last instance inside the launchsettings.json file (regardless of what I choose in the drop down as shown above in the screenshot). i.e. It is returning a value of "prod". Again, I want to mention that this is an NUNIT NET CORE project and I am using the Test Explorer to Run and Debug tests (See screenshot below)
Also, I dont know if the below code is relavent, but the below is how I set the environment variables...
public class LaunchSettingsFixture
{
public LaunchSettingsFixture()
{
using (var file = File.OpenText("Properties\\launchSettings.json"))
{
var reader = new JsonTextReader(file);
var jObject = JObject.Load(reader);
var variables = jObject
.GetValue("profiles")
//select a proper profile here
.SelectMany(profiles => profiles.Children())
.SelectMany(profile => profile.Children<JProperty>())
.Where(prop => prop.Name == "environmentVariables")
.SelectMany(prop => prop.Value.Children<JProperty>())
.ToList();
foreach (var variable in variables)
{
Environment.SetEnvironmentVariable(variable.Name, variable.Value.ToString());
}
}
}
}