I'm deploying several Web Apps to Azure: one to a development/test environment, and one to production.
I tried determining the appsettings to load based on the environment computer name, because my understanding is that Core is different in that you can't publish for a specific environment. I didn't realize that the computer name changes in Azure, so when I tried deploying an older application to the Test environment earlier today, it started using Production settings.
public static void Main(string[] args)
{
string environmentName;
if (args.Length > 0)
{
environmentName = args[0];
if (environmentName != "Local" && environmentName != "Test" && environmentName != "Production")
{
Console.WriteLine("Invalid environment. Please choose between Local, Test, or Production.");
environmentName = Console.ReadLine();
}
}
else
{
Console.WriteLine("Application was started without arguments.");
if (Environment.GetEnvironmentVariable("COMPUTERNAME").Contains("dev"))
{
environmentName = "Local";
}
else if (Environment.GetEnvironmentVariable("COMPUTERNAME").Contains([Azure name]))
{
environmentName = "Test";
}
else
{
environmentName = "Production";
}
}
}
public static IHostBuilder CreateHostBuilder(string[] args, string environment) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(configApp =>
{
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
configApp.SetBasePath(path);
configApp.AddJsonFile($"appsettings.{environment}.json", false, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog();
What can I do to make sure that my test Azure uses the test environment, and my production uses production?